Function object (functor functor)

Source: Internet
Author: User

Simply put, a function object is an instance of a class that overloads the () operator, which can be used like a function.

#include <iostream>using namespacestd;classadd{ Public:    int operator()(Const int&a,Const int&b) {return(A +b); }    Double operator()(Const Double&a,Const Double&b) {return(A +b); }};voidMain () {ADD plus; cout<< Plus ( $, Wu) << Endl;// Aboutcout << Plus (44.3,58.4) << Endl;//102.7}

Using templates:

#include <iostream>using namespacestd;template<typename t>classadd{ Public: Toperator()(ConstT &a,ConstT &b) {return(A +b); }};voidMain () {ADD<int>Plus1; cout<< Plus1 ( $, Wu) << Endl;// Aboutadd<Double>Plus2; cout<< Plus2 (44.3,58.4) << Endl;//102.7}

The find_if, count_if, For_each, etc. in the standard library are related to:

#include <iostream>#include<algorithm>#include<vector>using namespacestd;template<typename t>classshowt{ Public:    void operator()(ConstT &a) {cout<< a <<'\ t'; }};voidShowConst int&a) {cout<< a <<'\ t';}voidmain () {vector<int> VEC = {1,3,5,7,9, -, -,156, $, the}; //For_each (Vec.begin (), Vec.end (), show); //OKFor_each (Vec.begin (), Vec.end (), showt<int> ());//OKcout <<Endl;}

Unlike a normal function, a class can have a data member, so the functor can have a state:

#include <iostream>#include<algorithm>#include<vector>using namespacestd;template<typename t>classaddt{T m_data=0; Public:    void operator()(ConstT &a) {m_data+=A; } T result ()Const    {        returnm_data; }};voidmain () {vector<int> VEC = {1,3,5,7,9, -, -,156, $, the}; AddT<int> sum = For_each (Vec.begin (), Vec.end (), addt<int> ());//For_each Returns the "copy" of the third parameter (A copy of the function object)cout <<Sum.result ()<<Endl;}
//function function: Performs _func on an interval [beg,end]. and returns _FUNC.template<class_init,class_fn1>inline _fn1 _for_each (_init _first, _init _last, _fn1 _func) { for(; _first! = _last; + +_first) _func (*_first); return(_func);} Template<class_init,class_fn1>inline _fn1 for_each (_init _first,//Start Interval_init _last,//Endinterval, [_first,_last]_FN1_FUNC)//function object or function (unary_function){    //perform function for each element_debug_range (_first, _last);    _debug_pointer (_func); return(_for_each (_unchecked (_first), _unchecked (_last), _func));}
For_each
template<class_init,class_pr>inline _init _find_if (_init _first, _init _last, _pr _pred) {//Find first satisfying _pred             for(; _first! = _last; + +_first)if(_pred (*_first))//bool (*pfun) (T)                     Break; return(_first); } template<class_init,class_pr>inline _init find_if (_init _first, _init _last, _pr _pred) {//Find first satisfying _pred_debug_range (_first, _last);            _debug_pointer (_pred); return(_rechecked (_first, _find_if (_unchecked (_first), _unchecked (_last), _pred))); }
find_if
template<class_init,class_pr>Inline TypeName Iterator_traits<_InIt>::d ifference_type _count_if (_init _first, _init _last, _pr _pred) {//count elements satisfying _predTypeName Iterator_traits<_init>::d ifference_type _count =0;  for(; _first! = _last; + +_first)if(_pred (*_first)) ++_count; return(_count); } template<class_init,class_pr>Inline TypeName Iterator_traits<_InIt>::d ifference_type count_if (_init _first, _init _last, _pr _pred) {//count elements satisfying _pred_debug_range (_first, _last);            _debug_pointer (_pred); return(_count_if (_unchecked (_first), _unchecked (_last), _pred)); }
count_if

Function object (functor functor)

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.