std::count, std::count_if
Defined in header
<algorithm>
|
||
template< class InputIt, class T >
typename iterator_traits<InputIt>::difference_type |
(1) | |
template< class ExecutionPolicy, class InputIt, class T >
typename iterator_traits<InputIt>::difference_type |
(2) | (since C++17) |
template< class InputIt, class UnaryPredicate >
typename iterator_traits<InputIt>::difference_type |
(3) | |
template< class ExecutionPolicy, class InputIt, class UnaryPredicate >
typename iterator_traits<InputIt>::difference_type |
(4) | (since C++17) |
Returns the number of elements in the range [first, last)
satisfying specific criteria.
value
.p
returns true.policy
. These overloads do not participate in overload resolution unless std::is_execution_policy_v<std::decay_t<ExecutionPolicy>> is true
Contents |
[edit] 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 signature of the predicate function should be equivalent to the following: bool pred(const Type &a); The signature does not need to have const &, but the function must not modify the objects passed to it. |
Type requirements | ||
-
InputIt must meet the requirements of InputIterator .
|
[edit] Return value
number of elements satisfying the condition.
[edit] Complexity
exactly last
- first
comparisons / applications of the predicate
[edit] 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,
-
- if
policy
is std::parallel_vector_execution_policy, std::terminate is called - if
policy
is std::sequential_execution_policy or std::parallel_execution_policy, the algorithm exits with an std::exception_list containing all uncaught exceptions. If there was only one uncaught exception, the algorithm may rethrow it without wrapping in std::exception_list. It is unspecified how much work the algorithm will perform before returning after the first exception was encountered. - if
policy
is some other type, the behavior is implementation-defined
- if
- If the algorithm fails to allocate memory (either for itself or to construct an std::exception_list when handling a user exception), std::bad_alloc is thrown.
[edit] Notes
For the number of elements in the range [first, last)
without any additional criteria, see std::distance.
[edit] Possible implementation
First version |
---|
template<class InputIt, class T> typename iterator_traits<InputIt>::difference_type count(InputIt first, InputIt last, const T& value) { typename iterator_traits<InputIt>::difference_type ret = 0; for (; first != last; ++first) { if (*first == value) { ret++; } } return ret; } |
Second version |
template<class InputIt, class UnaryPredicate> typename iterator_traits<InputIt>::difference_type count_if(InputIt first, InputIt last, UnaryPredicate p) { typename iterator_traits<InputIt>::difference_type ret = 0; for (; first != last; ++first) { if (p(*first)) { ret++; } } return ret; } |
[edit] Example
The following code uses count
to determine how many integers in a std::vector match a target value.
#include <algorithm> #include <iostream> #include <vector> int main() { int data[] = { 1, 2, 3, 4, 4, 3, 7, 8, 9, 10 }; std::vector<int> v(data, data+10); int target1 = 3; int target2 = 5; int num_items1 = std::count(v.begin(), v.end(), target1); int num_items2 = std::count(v.begin(), v.end(), target2); std::cout << "number: " << target1 << " count: " << num_items1 << '\n'; std::cout << "number: " << target2 << " count: " << num_items2 << '\n'; }
Output:
number: 3 count: 2 number: 5 count: 0
This example uses a lambda expression to count elements divisible by 3.
#include <algorithm> #include <iostream> #include <vector> int main() { int data[] = { 1, 2, 3, 4, 4, 3, 7, 8, 9, 10 }; std::vector<int> v(data, data+10); int num_items1 = std::count_if(v.begin(), v.end(), [](int i) {return i % 3 == 0;}); std::cout << "number divisible by three: " << num_items1 << '\n'; }
Output:
number divisible by three: 3
[edit] See also
(parallelism TS)
|
parallelized version of std::count (function template) |
(parallelism TS)
|
parallelized version of std::count_if (function template) |