Count STL Algorithm
The count() algorithm returns the number of elements in the sequence beginning at start and ending at end that match val.
template< class InputIterator, class T > typename iterator_traits<InputIterator>::difference_type count( InputIterator first, InputIterator last, const T &value ); Count appearances of value in range (function template) Returns the number of elements in a range whose values match a specified value. Returns the number of elements in the range [first, last) matching given value value.
Implementation of count() algorithm
#include <iostream> #include <algorithm> #include <vector> using namespace std; typedef int type; const int size=10; int main () { type const list[size]= {1,3,5,9,2,6,7,2,9,0}; vector<type> vec0(list, list+10); type result; for ( int k=0; k< vec0.size(); k++ ) { result = count(vec0.begin(), vec0.end(), vec0[k]); cout << "number: " << vec0[k] << " count: " << result << endl; } return 0; }// end of main
Program Output:
number: 1 count: 1 number: 3 count: 1 number: 5 count: 1 number: 9 count: 2 number: 2 count: 2 number: 6 count: 1 number: 7 count: 1 number: 2 count: 2 number: 9 count: 2 number: 0 count: 1