function template
<algorithm>
std::count
template <class InputIterator, class T> typename iterator_traits<InputIterator>::difference_type count (InputIterator first, InputIterator last, const T& val);
Count appearances of value in range
[first,last) that compare equal to val.
The function uses operator== to compare the individual elements to val.
The behavior of this function template is equivalent to:
|
|
Parameters
- first, last
- Input iterators to the initial and final positions of the sequence of elements. The range used is
[first,last), which contains all the elements between first and last, including the element pointed by first but not the element pointed by last. - val
- Value to match.
T shall be a type supporting comparisons with the elements pointed by InputIterator usingoperator==(with the elements as left-hand side operands, and val as right-hand side).
Return value
The number of elements in the range[first,last) that compare equal to val.The return type (iterator_traits<InputIterator>::difference_type) is a signed integral type.
Example
|
|
Output:
10 appears 3 times. 20 appears 3 times.
Complexity
Linear in the distance between first and last: Compares once each element.Data races
The objects in the range[first,last) are accessed (each object is accessed exactly once).Exceptions
Throws if either an element comparison or an operation on an iterator throws.Note that invalid arguments cause undefined behavior.
See also
- for_each
- Apply function to range (function template)
- count_if
- Return number of elements in range satisfying condition (function template)
- find
- Find value in range (function template)
- replace
- Replace value in range (function template)