Successor to iterator

I want to initialize an iterator (of an arbitrary type) with a successor to another iterator (of the same type). The following code works with random access iterators, but it does not work with forwards or bidirectional iterators:

Iterator i = j + 1; 

A simple workaround is:

 Iterator i = j; ++i; 

But this does not work as an init-stament for loop. I could use a function template as follows:

 template <typename Iterator> Iterator succ(Iterator it) { return ++it; } 

and then use it like this:

 Iterator i = succ(j); 

Is there anything similar in STL or Boost, or is there an even better solution that I don't know about?

+7
source share
1 answer

I think you are looking for next in Boost.Utility . It also has prior to get an iterator to the previous item.

Update:

C ++ 11 introduced std::next and std::prev .

+14
source

All Articles