C ++ STL's non-mutating algorithms is a set of template functions that do not destroy operation data, it is used for processing sequence data one by one, element search, subsequence search, statistics, and matching.
The Count algorithm is used to calculate the number of occurrences of a given value in the container. Two prototypes are used to calculate the number of elements equal to the value on the [first, last) range of the iterator n. The difference is whether the Count N is returned directly or by reference.
Function prototype:
template<class InputIterator, class Type> typename iterator_traits<InputIterator>::difference_type count( InputIterator _First, InputIterator _Last, const Type& _Val );
template<class InputIterator, class T> inline size_t count( InputIterator First, InputIterator Last, const T& Value )
Sample Code:
/*************************************** * **************************** Copyright (c) jerry Jiang * file name: count. CPP * Author: Jerry Jiang * Create Time: 2011-10-6 20:25:37 * mail: jbiaojerry@gmail.com * blog: http://blog.csdn.net/jerryjbiao * Description: simple program interpretation of C ++ STL algorithm series of six * non variable algorithm: count the number of container elements equal to a value ******************************* * *********************************/# pragma warning (disable: 4786) # include <iostream> # include <algorithm> # include <list> # include <string> # include <vector> using namespace STD; int main () {list <int> ilist; For (list <int>: size_type Index = 0; index <100; ++ index) {ilist. push_back (index % 20);} List <int>: difference_type num = 0; int value = 9; num = count (ilist. begin (), ilist. end (), value); cout <"number of elements in the Linked List that are equal to value:" <num <Endl; vector <string> vecstring; vecstring. push_back ("this"); vecstring. push_back ("is"); vecstring. push_back ("A"); vecstring. push_back ("test"); vecstring. push_back ("program"); vecstring. push_back ("is"); string valstring ("is"); ptrdiff_t result = count (vecstring. begin (), vecstring. end (), valstring); cout <"number of elements in the container whose elements are is:" <result <Endl; return 0 ;}
**************************************** **************************************** **************************************** *******
C ++ classic bibliography index and resource download:Http://blog.csdn.net/jerryjbiao/article/details/7358796
**************************************** **************************************** **************************************** ********