There are times when I'll want to use the iterator returned by a function from the algorithm library. The problem I'm facing occurs when I go between a modifying function and a non-modifying function. Cause in the non-modifying function I want to use a const_iterator
. As a toy example:
vector<int> v = { 1, 8, 7, 4, 3, 6, 2, 5 };
auto it = partition(begin(v), end(v), bind(greater<int>(), placeholders::_1, 3));
cout << (find(cbegin(v), it, 13) != cend(v)) << endl;
When I try to compile this code I get the error:
no matching function for call to
find(std::vector<int>::const_iterator, __gnu_cxx::__normal_iterator<int*, std::vector<int> >&, int)
The problem I'm running into is the only conversion process I can find is potentially expensive: auto cit = next(cbegin(v), distance(begin(v), it))
Is there a way I can make this work? Or am I stuck converting or just using non-const_iterator
s?
It's much less expensive to simply cast the mutable iterator to a constant iterator:
cout << (find(cbegin(v), vector<int>::const_iterator{it}, 13)
!= cend(v)) << endl;
A mutable iterator should always be castable into a constant iterator.
EDIT: I found the part of the standard that guarantees that an iterator is convertible to a constant iterator.
Table 96 in section 23.2 "Container requirements" specifies that the expression X::iterator
results in:
any iterator category that meets the forward iterator requirements. convertible to X::const_iterator.