Getting the next element in a set without incrementing the iterator?

Hi all. I'm trying to do something like this:

1
2
3
4
5
6
7
8
std::set<int> mySet;

//*********
//insert some elements
//*********

std::set<int>::iterator first = mySet.begin();
std::set<int>::iterator second = first + 1;


but my compiler says nono, no + operator.

I see the ++ and -- operators, but that would change what first points to, which I want to stay the same.
Why not copy the iterator?

1
2
second = first;
++second;
Last edited on
Or std::set<int>::iterator second = first++;, for that matter.
PanGalactic:
That will have to do I think.

Athar:
Does not first++ CHANGE what first points to? ie, first != (first++)
The point is, I do not want to increment the first pointer, just get to the next element coming after it.
Take a look at http://www.cplusplus.com/reference/std/iterator/advance/

Seem like the advance function help to navigate our iterator around. You can say advance(itr, 1) to get what you want. Then you can advance(itr, -1) to go back to previous position.
Does not first++ CHANGE what first points to?

No, a correct implementation of the postfix increment operator will never change the object.
first != (first++) is true, but not because first is changed. A temporary, incremented iterator will be returned by first++.
Topic archived. No new replies allowed.