c++ - 无法将 std::lower_bound 返回值分配给 vector::iterator
问题描述
传递给函数: const std::vector &xv:
std::vector<float>::iterator lowBound = lower_bound(xv.begin(), xv.end(), x);
std::vector<float>&
在我将其更改为之前,该功能运行良好const std::vector<float>&
Lower_bound
在编译时返回语法错误说:
no suitable user-defined conversion from "__gnu_cxx::__normal_iterator<const float *, std::vector<float, std::allocator<float>>>" to "__gnu_cxx::__normal_iterator<float *, std::vector<float, std::allocator<float>>>" exists
std::vector<...>::const_iterator std::lower_bound<std::vector<float, std::allocator<float>>::const_iterator, float>(std::vector<...>::const_iterator __first, std::vector<...>::const_iterator __last, const float &__val)
我尝试过更改std::vector<float>::iterator
为const std::vector<float>::iterator
并且也尝试过std::vector<const float>::iterator
。
这些选项似乎都不起作用。
我很抱歉给您带来麻烦,我尝试用谷歌搜索错误,但无法找到答案。
解决方案
lower_bound
返回一个const_iterator
。你需要
std::vector<float>::const_iterator lowBound = lower_bound(xv.begin(), xv.end(), x);
或者简单地说 (C++11)
auto lowBound = lower_bound(xv.begin(), xv.end(), x);
推荐阅读
- c - 用于 Windows 10 的 MSYS2 中的 Link -static GTK+-2.0 程序(使用 mingw64,而不是 mingw32)
- swift - 未找到 ProjectName-Swift.h / 生成的 Xcode 10
- javascript - 如果我通过 Javascript 从文档头中删除 Javascript 脚本会发生什么?
- javascript - 急需帮助:Ajax.beginform 调用两次,第二次调用之前的值
- applescript - Applescript 检查用户输入的第一个字母
- sql - ISNULL 没有帮助数据在此查询中显示为空字符串
- javascript - R:networkD3添加缩放功能
- swift - 如何用数据填充表格视图单元格?
- azure - 为什么 sendgrid 营销部门会未经授权
- angular - 如果复选框选中角度 6,则启用文本框