I have a method which finds the particular position in a map and 'returns' this via an iterator reference:
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;
}
I am getting a compiler error because std::upper_bound()
is returning an std::const_iterator
and comparing this with an std::iterator
.
Should I 'cast' the return value of upper_bound()
to be non-const?
No, you should not cast the return value of
upper_bound
. You should remove theconst
qualifier on the function. The function is providing non-const access to an element of the map (via the out parameter), so it should not be const.