6

Say I have an iterator it which is pointing to some element of map.
Also I have another iterator it1 , and I want to do something like this

it1 = it + 1;  

How can we achieve this in C++ as above statement gives error in C++.

3
  • 2
    Bidirectional iterators don't support operator+. std::next would do that. Commented Nov 2, 2012 at 19:53
  • 2
    @chris That's an answer not a comment Commented Nov 2, 2012 at 19:54
  • @Dave, For some reason, I was considering the possibility of using two different iterator types, but looking back, that seems pretty unlikely. Commented Nov 2, 2012 at 19:58

1 Answer 1

5

In C++11, you say auto it1 = std::next(it, 1);.

Prior to that, you have to say something like:

std::map<K, T>::iterator it1 = it;
std::advance(it1, 1);

Don't forget to #include <iterator>.

Sign up to request clarification or add additional context in comments.

1 Comment

Note you can just say std::next(it), the second param is defaulted to 1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.