Jonathan Mee
Jonathan Mee

Reputation: 38939

Is There a make_const_iterator?

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_iterators?

Upvotes: 2

Views: 361

Answers (3)

Sam Varshavchik
Sam Varshavchik

Reputation: 118425

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.

Upvotes: 3

Vlad from Moscow
Vlad from Moscow

Reputation: 311048

There are three approaches.

The first one is to write

cout << (find( begin(v), it, 13) != cend(v)) << endl;
               ^^^^^

The second one is to write

cout << (find(cbegin(v), static_cast<std::vector<int>::const_iterator>( it )
, 13) != cend(v)) << endl;

Or shorter

cout << (find(cbegin(v), static_cast<decltype( v.cbegin())>( it )
, 13) != cend(v)) << endl;

And the third one is to write

cout << (find<std::vector<int>>::const_iterator>( cbegin(v), it, 13) != cend(v)) << endl;

or shorter

cout << (find<decltype( v.cbegin())>( cbegin(v), it, 13) != cend(v)) << endl;

Upvotes: 1

clcto
clcto

Reputation: 9648

You can specify the template argument:

find<decltype(cbegin(v))>(cbegin(v), it, 13) != cend(v)

Demo

Upvotes: 4

Related Questions