【发布时间】:2017-05-02 11:44:42
【问题描述】:
我有一个方法可以找到地图中的特定位置并通过迭代器引用“返回”它:
bool Func(const int searchKey, MyMap::iterator& iter) const {
iter = _map.upper_bound(searchKey); // Compiler error: comparing non-const iterator and const iterator
const bool found = iter != _map.begin();
if(something){
--_map;
return true;
}
return false;
}
我收到编译器错误,因为 std::upper_bound() 返回一个 std::const_iterator 并将其与 std::iterator 进行比较。
我应该将upper_bound() 的返回值“转换”为非常量吗?
【问题讨论】:
-
为了清楚起见 -
_map是成员变量,而这是成员函数? -
_map是什么?请发minimal reproducible example。
标签: c++ stl iterator constants stdmap