incrementing an STL list interator

  • Thread starter Thread starter Abubakar
  • Start date Start date
A

Abubakar

Hi,

lets say I have a iterator of the type:
list<string>::const_iterator itr;
lets say its at some position in a given list. Now I can goto the next item
by simply doing a itr++. But I want to increment more than just 1. Lets says
I want to increment the itr 3 times, ie being able to do something like
itr+=3. But as the += is not defined for that iterator class, I cant do
that. What should I do?

Regards,

-ab.
 
Hi,

lets say I have a iterator of the type:
list<string>::const_iterator itr;
lets say its at some position in a given list. Now I can goto the next item
by simply doing a itr++. But I want to increment more than just 1. Lets says
I want to increment the itr 3 times, ie being able to do something like
itr+=3. But as the += is not defined for that iterator class, I cant do
that. What should I do?

Look into std::advance. It can handle any iterator type and will repeatedly
increment non-random access iterators as necessary. The operator isn't
provided for non-random access iterators to emphasize that increment values
 
Wow, pretty cool. Thanks :)

-ab.

Doug Harrison said:
Look into std::advance. It can handle any iterator type and will
repeatedly
increment non-random access iterators as necessary. The operator isn't
provided for non-random access iterators to emphasize that increment
values
 
Back
Top