C ++ standard library-function object (functor function imitation)

Source: Internet
Author: User
// The "function parameters" passed to the algorithm may not necessarily be functions, but objects that act like functions. This type of object is called a function object or a functor (function-like) object ).
// C ++ standard library 5.9 functor = function object // # include <iostream> # include <vector> # include <algorithm> using namespace STD; class printint {public: void operator () (int elem) const {cout <ELEM <";}}; template <class iterator, class operation> operation for_each _ (iterator act, iterator end, Operation OP) {While (act! = END) {OP (* Act); ++ Act;} return op;} int main () {vector <int> F; vector <int >:: iterator fit; printint pint; For (INT I = 0; I <10; ++ I) {f. push_back (I);} // for_each (F. begin (), F. end (), printint ();/* For (fit = f. begin (); fit! = F. end (); ++ fit) {pint (* fit); // call through the object} */for_each _ (F. begin (), F. end (), printint (); // a temporary printint type object will be generated. Return 0 ;}

 

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.