Simple Program interpretation of C ++ STL algorithm series 6: Count

Source: Internet
Author: User

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

**************************************** **************************************** **************************************** ********

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.