Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ how to skip next 2 iterations of loop i.e. multiple continue

Tags:

c++

Say I have the following loop:

vector <string> args;
for (string s : args)
{
    if ( s == "condition" )
        continue; // skips to next iteration
}

How can I skip multiple iterations in this instance? Is there something like multiple continue statements?

like image 661
Char Avatar asked Sep 02 '25 17:09

Char


2 Answers

Consider using for loop with index:

for (size_t i = 0; i < args.size(); i++)
{
    if (args[i] == "condition") {
        i++;
        continue;
    }
}
like image 105
zhm Avatar answered Sep 04 '25 06:09

zhm


You can use iterator.

auto it_end = --args.end();
for(auto it = args.begin(); it != args.end(); it++){
    if ( *it == "condition" && it != it_end) it++;
}
like image 28
Jiahao Cai Avatar answered Sep 04 '25 06:09

Jiahao Cai