A simple program interprets one of the C ++ STL algorithm series: for_each

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.

For_each is used to traverse container elements one by one. It executes operations defined by the single-Parameter Function object F for each element specified by [first, last) in the iterator range.

Prototype:

template<class InputIterator, class Function>   Function for_each(      InputIterator _First,       InputIterator _Last,       Function _Func      );

Note:

In the for_each algorithm range [first, last), each element calls function f and returns the input parameter F. This function does not modify any element in the sequence.

Sample Code:

/*************************************** * **************************** Copyright (c) jerry Jiang ** file name: for_each.cpp * Author: Jerry Jiang * Create: 2011-9-27 19:46:44 * mail: jbiaojerry@gmail.com * blog: http://blog.csdn.net/jerryjbiao ** description: A simple program interprets one of the C ++ STL algorithm series * Non-Variable Algorithms: traverse the container element one by one: for_each *********************************** * ******************************/# include <algorithm> # include <list> # include <iostream> using namespace STD; // print is the imitation function struct print {int count; print () {COUNT = 0 ;}void operator () (int x) {cout <x <Endl; + + Count ;}; int main (void) {list <int> ilist; // initialize for (size_t I = 1; I <10; ++ I) {ilist. push_back (I);} // traverse the ilist element and print Print P = for_each (ilist. begin (), ilist. end (), print (); // print the number of ilist elements. cout <p. count <Endl; return 0 ;}

Example:

Imitation functions, or function objects, are one of the six components (containers, configurators, iterators, algorithms, adapters, and functions) of STL (standard template library; although the imitation function is small, it greatly expands the functions of the algorithm. Almost all algorithms have the imitation function version. For example, the search algorithm find_if is an extension of the find algorithm. The standard search is achieved by two element directions, but what is equal requires different definitions under different circumstances, if the address is equal, the address and the zip code are both equal. Although these definitions are changing, the algorithm itself does not need to be changed, thanks to the imitation function. The function object is called a function object because all the functions are classes that define () function operators.

Reference articles on Imitation functions:

1. Essentials of using the imitation Function

2. What is a function imitation?

3. c ++ functions (functor)

 

 

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

C ++ classic bibliography index and resource download:Http://blog.csdn.net/jerryjbiao/article/details/7358796

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

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.