On this page
std::count, std::count_if
Defined in header <algorithm> |
||
|---|---|---|
| (1) | ||
|
(until C++20) | |
|
(since C++20) | |
|
(2) | (since C++17) |
| (3) | ||
|
(until C++20) | |
|
(since C++20) | |
|
(4) | (since C++17) |
Returns the number of elements in the range [first, last) satisfying specific criteria.
value (using operator==).
p returns true.
policy. These overloads do not participate in overload resolution unless
|
(until C++20) |
|
(since C++20) |
Parameters
| first, last | - | the range of elements to examine |
| value | - | the value to search for |
| policy | - | the execution policy to use. See execution policy for details. |
| p | - | unary predicate which returns true for the required elements. The expression |
| Type requirements | ||
-InputIt must meet the requirements of LegacyInputIterator. |
||
-ForwardIt must meet the requirements of LegacyForwardIterator. |
||
Return value
The number of iterators it in the range [first, last) satisfying the following condition:
*it == value is true.
p(*it) != false is true.
Complexity
Given N as std::distance(first, last):
N comparisons with value using operator==.
N applications of the predicate p.
Exceptions
The overloads with a template parameter named ExecutionPolicy report errors as follows:
- If execution of a function invoked as part of the algorithm throws an exception and
ExecutionPolicyis one of the standard policies,std::terminateis called. For any otherExecutionPolicy, the behavior is implementation-defined. - If the algorithm fails to allocate memory,
std::bad_allocis thrown.
Notes
For the number of elements in the range [first, last) without any additional criteria, see std::distance.
Possible implementation
See also the implementations of count in libstdc++ and libc++.
See also the implementations of count_if in libstdc++ and libc++.
| count |
|---|
|
| count_if |
|
Example
#include <algorithm>
#include <array>
#include <iostream>
#include <iterator>
int main()
{
constexpr std::array v{1, 2, 3, 4, 4, 3, 7, 8, 9, 10};
std::cout << "v: ";
std::copy(v.cbegin(), v.cend(), std::ostream_iterator<int>(std::cout, " "));
std::cout << '\n';
// Determine how many integers match a target value.
for (const int target : {3, 4, 5})
{
const int num_items = std::count(v.cbegin(), v.cend(), target);
std::cout << "number: " << target << ", count: " << num_items << '\n';
}
// Use a lambda expression to count elements divisible by 4.
int count_div4 = std::count_if(v.begin(), v.end(), [](int i) { return i % 4 == 0; });
std::cout << "numbers divisible by four: " << count_div4 << '\n';
// A simplified version of `distance` with O(N) complexity:
auto distance = [](auto first, auto last)
{
return std::count_if(first, last, [](auto) { return true; });
};
static_assert(distance(v.begin(), v.end()) == 10);
}
Output:
v: 1 2 3 4 4 3 7 8 9 10
number: 3, count: 2
number: 4, count: 2
number: 5, count: 0
numbers divisible by four: 3
Defect reports
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
| DR | Applied to | Behavior as published | Correct behavior |
|---|---|---|---|
| LWG 283 | C++98 | T was required to be EqualityComparable, butthe value type of InputIt is not always T |
removed the requirement |
See also
| returns the distance between two iterators (function template) |
|
|
(C++20)(C++20)
|
returns the number of elements satisfying specific criteria (niebloid) |
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
https://en.cppreference.com/w/cpp/algorithm/count