In some code that I was working on, I have a for loop that iterates through a map:
for (auto it = map.begin(); it != map.end(); ++it) {
And I wondered if there was any way to write something briefly into action:
for (auto it = map.begin(); it != map.end(); ++it) { //do stuff here } else { //Do something here since it was already equal to map.end() }
I know that I can rewrite it as:
auto it = map.begin(); if (it != map.end(){ while ( it != map.end() ){ //do stuff here ++it; } } else { //stuff }
But is there a better way that isn't related to wrapping into an if statement?
source share