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: A simple program interpreted six of the C ++ STL algorithm series
* Non-variable algorithm: count the number of container elements equal to a certain 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 whose elements are equal to value in the linked list :"
<Num <endl;
Vector <string> vecString;
VecString. push_back ("this ");
VecString. push_back ("is ");
VecString. push_back ("");
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 element is :"
<Result <endl;
Return 0;
} Excerpt from: Jerry Jiang's program life