Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Iterate from the second element of a map

Tags:

c++

stl

multimap

I have a std::multimap on which I am iterating using a forward iterator.

std::multimap<int,char>::iterator it;
for(it=map.begin();it!=map.end();++it) {
    // do something
}

Now I need to treat the first element differently and start iterating from the second element of the map. How do I do that?

like image 527
Abhishek Chanda Avatar asked Oct 01 '12 19:10

Abhishek Chanda


1 Answers

std::multimap<int,char>::iterator it;

for(it = std::next(map.begin()); it != map.end(); ++it) {
    // do something
}

This is C++11 only. You'll need to include <iterator>.

The other option is obvious, but less pretty:

it = map.begin();
++it;
for(; it != map.end(); ++it) {
    // do something
}

Take a look at std::advance, too.

like image 153
jrok Avatar answered Oct 12 '22 06:10

jrok