STL all_of and any_of in C++

Level 4246 keystrokes

Checks with all_of whether every element is even and with any_of whether at least one is greater than 7; both are true, and cout prints bools as 1 1.

C++ code

1vector<int> nums = {2, 4, 6, 8, 10};
// all_of is true only if the predicate holds for every element
2bool allEven = all_of(nums.begin(), nums.end(),
// Predicate: is n even? (true for all five)
3 [](int n) { return n % 2 == 0; });
// any_of is true if the predicate holds for at least one element
4bool anyGreater = any_of(nums.begin(), nums.end(),
// Predicate: n > 7 (8 and 10 match)
5 [](int n) { return n > 7; });
// bool values are printed as 1 and 0 by default: prints 1 1
6cout << allEven << " " << anyGreater << endl;

Output

1 1
Practice typing this code

Practice typing this code. Results are not recorded in the rankings.

Symbols in this code

The symbols you type in this C++ code and how often they appear.

  • <× 9
  • ,× 8
  • (× 8
  • )× 8
  • ;× 6
  • =× 5
  • .× 4
  • {× 3
  • }× 3
  • _× 2

More C++ examples at level 4

← C++ typing practice