Example 1 — Count a number
Count how many times 2 appears.
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> v = {1, 2, 2, 3, 2};
int n = std::count(v.begin(), v.end(), 2);
std::cout << "2 appears " << n << " times\n";
return 0;
} How It Works
count()checks all five elements with==.- The values at index 1, 2 and 4 are equal to 2.
- The total, 3, is stored in
n.

