Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!
  • C++

Idiom #191 Check if any value in a list is larger than a limit

Given a one-dimensional array a, check if any value is larger than x, and execute the procedure f if that is the case

#include <algorithm>
if (std::any_of(a.begin(), a.end(), [x](auto y) { return y > x; }))
    f();

C++11 or later

a is an STL container
unsigned i;
for (i = 0; i < sizeof(a) / sizeof(a[0]); ++i) {
	if (a[i] > x) {
		f();
		break;
	}
}

New implementation...
< >
tkoenig