C ++ Iteration from the second map element

I have std::multimap on which I iterate using an advanced iterator.

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

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

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

This is only C ++ 11. You need to enable <iterator> .

Another option is obvious, but less cute:

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

Take a look at std::advance .

+11
source

It seems to look shorter

 it = ++map.begin(); 
+3
source
 std::multimap<int,char>::iterator it = map.begin(); //treat it here ++it for(;it!=map.end();++it) { } 
+2
source
 for(bool First=true, it=map.begin();it!=map.end();++it) { if (First) { do something; First=false; } else { do something else; } } 

or if you want:

 iterator it=map.begin(); if (it!=map.end()) { do something; ++it; } for (; it!=map.end(); ++it) { do something } 
+1
source

Change it = map.begin() in for-initializer to it = map.begin(), ++it .

+1
source

Source: https://habr.com/ru/post/1437274/


All Articles