STL Algorithm design Concepts-function objects and function objects when parameters and return values

Source: Internet
Author: User
Tags function prototype

Function object:
The class that overloads the function call operator. Their objects are often called function objects, meaning they are objects that behave like functions.

A class object that shows a feature of a function that uses a class object in the form of an object name + (List of tables), assuming that there is no context, it can be treated as a function.
This is done by overloading the operator () of the class.


"In the standard library. Function objects are widely used to obtain elasticity. " Very many algorithms in the standard library can use function objects or functions as custom callback behaviors.
Demo

#include <iostream> #include <cstdio>using namespace std;//function object. Class overloads () template <typename t>  class showelement{public://overload () void operator () (t &t) {cout << T < < Endl;} Protected:private:int n;};/ /function template Template <typename t>void funcshowelement (t &t) {cout << t << Endl;} normal function void FuncShowElement2 (int &t) {cout << t << Endl;} void Play01 () {int a = 10; Showelement<int> Showelement;showelement (a); The invocation of a function object, much like a function, is called a functor//10funcshowelement<int> (a); FuncShowElement2 (a);} int main () {play01 (); return 0;}
The above simply demonstrates the use of function objects and similarities and differences with the definition of common functions, see the following demo.

#include <iostream> #include <cstdio> #include <vector> #include <algorithm>using namespace std ;//Function object, class overloaded () template <typename T>class showelement{public:showelement () {n = 0;} overloaded () void operator () (t &t) {++n;cout << T << ';} void Printn () {cout << "N:" << n << Endl;} Protected:private:int n;};/ /function template Template <typename t>void funcshowelement (t &t) {cout << T << ';} normal function void FuncShowElement2 (int &t) {cout << T << ';} void Play01 () {int a = 10; Showelement<int> Showelement;showelement (a); The invocation of a function object, much like a function, is called a functor//10funcshowelement<int> (a); FuncShowElement2 (a);} The advantage of Function object//function object belongs to class object, can break the concept of function. Can keep the call state information void play02 () {vector<int> v;v.push_back (1); V.push_back (3); V.push_back (5); For_each (V.begin (), V.end (), showelement<int> ()); Anonymous function object, anonymous functor cout << endl;//1 3 5for_each (V.begin (), V.end (), FuncShowElement2); Rewrite class Showelem with callback function cout << endl;//1 3 5//Entshowelement<int> showelement;/* for_each function prototype Template<class _init,class _Fn1> inline_Fn1 For_each (_ InIt _first, _init _last, _fn1 _func) {//Perform function for each element_debug_range (_first, _last); _debug_pointer (_fun c); _for_each (_unchecked (_first), _unchecked (_last), _func); return (_std Move (_func));} *///can see that the function object passing of the for_each algorithm is the element value passing. Not quoted pass For_each (V.begin (), V.end (), showelement); cout << endl;showelement.printn (); So the print results here are not the 3//n:0//solution we expected. For_each finally the function object passed in the value returned showelement = For_each (V.begin (), V.end (), showelement); cout << Endl; SHOWELEMENT.PRINTN (); bingo//N:3}int Main () {//play01 ();p lay02 (); return 0;}
Conclusion: It is very important to know whether the value returned by the STL algorithm is an iterator or a predicate (function object).




STL Algorithm design Concepts-function objects and function objects when parameters and return values

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.