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

前端 未结 14 2238
野的像风
野的像风 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 01:58

    A simpler way to express your actual logic is with std::none_of:

    if (std::none_of(std::begin(foo), std::end(foo), bar))
        baz();
    

    If the range proposal for C++17 gets accepted, hopefully this will simplify to:

    if (std::none_of(foo, bar)) baz();
    

提交回复
热议问题