C++11 std::function Usage (c + + frequently asked question 17)

Source: Internet
Author: User

c++11 std::function Usage

Directly on the code:

Example 1:std::function feels like a function pointer that has wood

 #include <iostream> #include <functional> #include <map>using namespace std;//normal function int Add (int i, int j) {return i + j;} lambda expression Auto mod = [] (int i, int j) {return I% J;};//function object class struct Divide{int operator () (int denominator, int divisor) {return denominator/divisor;}}; void Main () {//More flexible Mapmap<char, function<int (int, int) >> binops;binops[' + ') = add;binops['-'] = minus< Int> (); binops[' * '] = [] (int i, int j) {return i * j;}; binops['/'] = divide (); binops['% '] = mod;cout << binops[' + '] (5) << endl;cout << binops['-'] (10, 5) & lt;< endl;cout << binops[' * ' [5] << endl;cout << binops['/'] (5) << endl;cout << b inops['% '] (5) << Endl;system ("pause");} 

The above code also briefly describes the "lambda expression","lambda expressions" (lambda expression) is an anonymous function , lambda expression based on the mathematical lambda calculus named, The lambda abstraction (lambda abstraction), which corresponds directly to it, is an anonymous function, that is, a function without a function name. A lambda expression can represent closures (notice differs from the traditional mathematical sense).

c++11 std::bind Usage

Example 2:

The use of std::bind ... Std::bind is more like a wrapper over an existing function (decoration)

#include <iostream> #include <functional> #include <typeinfo> #include <string>using namespace Std;int add1 (int i, int j, int k) {return i + j + k;}        Class Person {Public:person (const char* name): M_strname (name) {} void SayHello (const char* name) Const {    Std::cout << m_strname<< "Say:hello" << name << Std::endl;    } int operator () (int i, int j, int k) const {return i + j + k;    } static string GetClassName () {return string ("person"); } private:string m_strname;};/     * * */int main (void) {//bind global function Auto ADD2 = Std::bind (ADD1, std::p laceholders::_1, std::p laceholders::_2, 10);    function add2 = bind add1 function, parameter 1 is unchanged, parameter 2 is unchanged, parameter 3 is fixed to 10.    Std::cout << typeid (ADD2). Name () << Std::endl;     Std::cout << "Add2" = "<< add2 (1, 2) << Std::endl;        Std::cout << "\ n---------------------------" << Std::endl; The BIND member function is the person man ("Std::fuNction ");    Auto SayHello = Std::bind (&person::sayhello, man/* caller */, std::p laceholders::_1/* parameter 1*/);        SayHello ("Peter");    Auto Sayhellotopeter = Std::bind (&person::sayhello, man/* caller */, "Peter"/* fixed parameter 1*/);        Sayhellotopeter ();    Bind static member function Auto Getclassname= std::bind (&person::getclassname);        Std::cout << getclassname () << Std::endl;        Std::cout << "\ n---------------------------" << Std::endl; Bind operator function Auto add100 = Std::bind (&person::operator (), MAN, std::p laceholders::_1, std::p laceholders::_2, 10    0);        Std::cout << "add100 (1, 2) =" << add100 (1, 2) << Std::endl;    Note: You cannot use Std::bind () to bind an overloaded function system ("pause"); return 0;}

C++11 std::function Usage (c + + frequently asked question 17)

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.