Skip to content
Tutorials Center
My learning

Topics

C++ Reference ยท Algorithms

The std::count and std::count_if algorithms

Reference for C++ std::count and std::count_if: count matching elements in a range, the signed return type, complexity and the ranges versions.

std::count returns how many elements in a range are equal to a given value. std::count_if returns how many satisfy a predicate. Both visit every element exactly once, so they take linear time and work on any input range.

The result type is std::iterator_traits<It>::difference_type, which is a signed integer (usually std::ptrdiff_t). That matters when you compare it with size(), which is unsigned.

C++
auto zeros = std::count(v.begin(), v.end(), 0);
auto evens = std::count_if(v.begin(), v.end(), [](int x) { return x % 2 == 0; });

Try it

C++ Counting values and conditions

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

C++ Ranges versions and a typical pitfall

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

Syntax

C++
template<class InputIt, class T>
typename std::iterator_traits<InputIt>::difference_type
count(InputIt first, InputIt last, const T& value);

template<class InputIt, class Pred>
typename std::iterator_traits<InputIt>::difference_type
count_if(InputIt first, InputIt last, Pred pred);
Name Description
first, last The half-open range [first, last) to scan.
value Each element is compared with it using ==.
pred Called with each element; returns something convertible to bool.

Returns: the number of matching elements, a signed integer. For an empty range the result is 0.

Errors / undefined behavior: none for valid ranges. The predicate must not modify the elements.

Usage notes

  • Complexity: exactly last - first comparisons or predicate calls, O(n). There is no early exit; to know only whether a match exists use find or any_of, which stop at the first hit.
  • Signed result: comparing it directly with v.size() triggers a sign-compare warning. Cast one side or use std::ranges::count, which also returns a signed type.
  • Member count: std::set, std::map and the unordered containers have their own count(key), which is O(log n) or O(1) on average. Use it instead of the algorithm for key lookups. multiset and multimap return the number of equal keys.
  • Counting characters: std::count(s.begin(), s.end(), 'x') is the simplest way to count one character in a string.
  • Ranges versions (C++20): std::ranges::count and count_if accept a range and a projection, so you can count by member without a lambda.
  • Parallel overloads: an execution policy can be the first argument; see execution policies.
  • Counting all values at once (a histogram) is better done in a single pass with a std::map or std::unordered_map, not with one count call per value.