std::count,std::count_if (3) - Linux Manuals
std::count,std::count_if: std::count,std::count_if
NAME
std::count,std::count_if - std::count,std::count_if
Synopsis
Defined in header <algorithm>
template< class InputIt, class T >
typename iterator_traits<InputIt>::difference_type (until C++20)
count( InputIt first, InputIt last, const T &value );
template< class InputIt, class T >
constexpr typename iterator_traits<InputIt>::difference_type (since C++20)
count( InputIt first, InputIt last, const T &value );
template< class ExecutionPolicy, class ForwardIt, class T >
typename iterator_traits<ForwardIt>::difference_type (2) (since C++17)
count( ExecutionPolicy&& policy, ForwardIt first, ForwardIt last, const T &value ); (1)
template< class InputIt, class UnaryPredicate >
typename iterator_traits<InputIt>::difference_type (until C++20)
count_if( InputIt first, InputIt last, UnaryPredicate p );
template< class InputIt, class UnaryPredicate >
constexpr typename iterator_traits<InputIt>::difference_type (3) (since C++20)
count_if( InputIt first, InputIt last, UnaryPredicate p );
template< class ExecutionPolicy, class ForwardIt, class UnaryPredicate >
typename iterator_traits<ForwardIt>::difference_type (4) (since C++17)
count_if( ExecutionPolicy&& policy, ForwardIt first, ForwardIt last, UnaryPredicate p );
Returns the number of elements in the range [first, last) satisfying specific criteria.
1) counts the elements that are equal to value.
3) counts elements for which predicate p returns true.
2,4) Same as (1,3), but executed according to policy. This overload only participates in overload resolution if std::is_execution_policy_v<std::decay_t<ExecutionPolicy>> is true
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 - The expression p(v) must be convertible to bool for every argument v of type (possibly const) VT, where VT is the value type of InputIt, regardless of value_category, and must not modify v. Thus, a parameter type of VT&is not allowed
Type requirements
-
InputIt must meet the requirements of LegacyInputIterator.
-
ForwardIt must meet the requirements of LegacyForwardIterator.
Return value
number of elements satisfying the condition.
Complexity
exactly last - first comparisons / applications of the predicate
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 ExecutionPolicy is one of the standard_policies, std::terminate is called. For any other ExecutionPolicy, the behavior is implementation-defined.
* If the algorithm fails to allocate memory, std::bad_alloc is thrown.
Notes
For the number of elements in the range [first, last) without any additional criteria, see std::distance.
Possible implementation
First version
Second version
Example
// Run this code
Output:
See also
distance (function template)