Is there an equivalent to the “for … else” Python loop in C++?

前端 未结 14 2083
野的像风
野的像风 2021-01-31 01:21

Python has an interesting for statement which lets you specify an else clause.

In a construct like this one:

for i in foo:
  if         


        
14条回答
  •  执念已碎
    2021-01-31 02:01

    Something like:

    auto it = foo.begin(), end = foo.end();
    while ( it != end && ! bar( *it ) ) {
        ++ it;
    }
    if ( it != foo.end() ) {
        baz();
    }
    

    should do the trick, and it avoids the unstructured break.

提交回复
热议问题