Beginner's Guide

# Standard Library Existence QueriesExistence QueriesQuery

## `any_of` / `all_of` / `none_of``any`/`all`/`none_of`any/all/none_ofC++11

``````#include <vector>
#include <iostream>
#include <iomanip>
#include <algorithm>

int main () {
std::cout << std::boolalpha;
std::vector<int> v {0,2,9,1,3,8,5,2,9};
auto const check = [](int x) { return x >= 1; };
// in subrange (as shown in image):
std::cout << all_of (begin(v)+1, begin(v)+7, check) << '\n';  // true
std::cout << any_of (begin(v)+1, begin(v)+7, check) << '\n';  // true
std::cout << none_of(begin(v)+1, begin(v)+7, check) << '\n';  // false
// in entire vector:
std::cout << all_of (begin(v), end(v), check) << '\n';  // false
std::cout << any_of (begin(v), end(v), check) << '\n';  // true
std::cout << none_of(begin(v), end(v), check) << '\n';  // false
}``````
``````#include <vector>
#include <iostream>
#include <iomanip>
#include <algorithm>

int main () {
std::cout << std::boolalpha;
std::vector<int> v {0,2,9,1,3,8,5,2,9};
auto const check = [](int x) { return x >= 1; };
std::cout << std::ranges::all_of (v, check) << '\n';  // false
std::cout << std::ranges::any_of (v, check) << '\n';  // true
std::cout << std::ranges::none_of(v, check) << '\n';  // false
}``````

## `count` `→ `#occurencescount

``````#include <vector>
#include <iostream>
#include <algorithm>

int main () {std::vector<int> v {5,2,9,1,3,2,5,2,2,9};
// count in subrange (as shown in image):
auto n = count(begin(v)+1, begin(v)+8, 2);  // n = 3
std::cout << "n: " << n << '\n';
// count in entire vector:
auto m = count(begin(v), end(v), 2);  // m = 4
std::cout << "m: " << m << '\n';
}``````
``````#include <vector>
#include <iostream>
#include <algorithm>

int main () {std::vector<int> v {2,9,1,3,2,5,2};
std::cout << std::ranges::count(v, 3) << '\n';  // 1
std::cout << std::ranges::count(v, 2) << '\n';  // 3
std::cout << std::ranges::count(v, 7) << '\n';  // 0
}``````

## `count_if`` → `#elems(`f=true`)count_if

``````#include <vector>
#include <iostream>
#include <algorithm>

int main () {std::vector<int> v {5,4,9,1,3,2,5,6,8,9};
auto const is_even = [](int x) {   return !(x & 1); };
// count in subrange (as shown in image):
auto n = count_if (begin(v)+1, begin(v)+8, is_even);  // n = 3
std::cout << "n: " << n << '\n';
// count in entire vector:
auto m = count_if (begin(v), end(v), is_even);  // m = 4
std::cout << "m: " << m << '\n';
}``````
``````#include <vector>
#include <iostream>
#include <algorithm>

int main () {std::vector<int> v {4,9,1,3,2,5,6};
auto const is_even = [](int x) {   return !(x & 1); };
auto n = std::ranges::count_if(v, is_even);  // n = 3
std::cout << "n: " << n << '\n';
}``````