Breaking in std::for_each loop

You can use std::any_of (or std::all_of or std::none_of) e.g. like this:

std::vector<int> a;
// ...     
std::all_of(a.begin(), a.end(), [&](int val) { 
  // return false if you want to break, true otherwise
  });

However, this is a wasteful solution (return values are not really used for anything), and you’re better off writing you own loop.

Leave a Comment

File not found.