count
Returns the number of elements in a range whose values match a specified value.
template<class InputIterator, class Type>
typename iterator_traits<InputIterator>::difference_type count(
InputIterator _First,
InputIterator _Last,
const Type& _Val
);
Parameters
_First
An input iterator addressing the position of the first element in the range to be traversed._Last
An input iterator addressing the position one past the final element in the range to be traversed._Val
The value of the elements to be counted.
Return Value
The difference type of the InputIterator that counts the number of elements in the range [ _First, _Last ) that have value _Val.
Remarks
The operator== used to determine the match between an element and the specified value must impose an equivalence relation between its operands.
This algorithm is generalized to count elements that satisfy any predicate with the template function count_if.
Example
// alg_count.cpp
// compile with: /EHsc
#include <vector>
#include <algorithm>
#include <iostream>
int main()
{
using namespace std;
vector<int> v1;
vector<int>::iterator Iter;
v1.push_back(10);
v1.push_back(20);
v1.push_back(10);
v1.push_back(40);
v1.push_back(10);
cout << "v1 = ( " ;
for (Iter = v1.begin(); Iter != v1.end(); Iter++)
cout << *Iter << " ";
cout << ")" << endl;
vector<int>::iterator::difference_type result;
result = count(v1.begin(), v1.end(), 10);
cout << "The number of 10s in v2 is: " << result << "." << endl;
}
v1 = ( 10 20 10 40 10 )
The number of 10s in v2 is: 3.
Requirements
Header: <algorithm>
Namespace: std