Bind () adapter (Adapter)--Call global function and member function

Source: Internet
Author: User

Calling global functions

To invoke a global function program instance:

#include <iostream>#include <algorithm>#include <functional>#include <locale>#include <string>using namespace STD;using namespace STD::p laceholders;CharMy_toupper (Charc) {locale loc;return STD::use_facet<STD::ctype<Char> > (loc).ToUpper(c);}intMain () {stringS"Internationalization");stringSub"Nation");string:: Iterator Pos; pos = Search (S.begin (), S.end (), Sub.begin (), Sub.end (), Bind (equal_to<Char> (), Bind (My_toupper, _1), bind (My_toupper, _2));if(pos! = S.end ()) {cout<<"\""<< Sub <<"\" is part of\ ""<< s <<"\""<< Endl; } System ("Pause");}
运行结果:"Nation"isof"Internationalization"请按任意键继续. . .

Program Analysis:
This example uses the search () algorithm to verify that the sub is a substring of s and is not case-insensitive. With the following:

bind(equal_to<char>(),bind(my_toupper, _1),bind(my_toupper, _2))

is to create a function object that is equivalent to calling:

my_toupper(arg1) == my_toupper(arg2);

Attention:
Bind () internally copies the arguments that are passed in.
To change this behavior, let the function object use a reference (reference) to point to the argument being passed in, either with ref () or CREF ():
For example:

void incr(int& i){    ++i;}int i =0;bind(incr, i)();      //仅仅一个拷贝ref//引用传递
Calling member functions

The following procedure demonstrates how bind () is used to invoke member functions

#include <iostream>#include <functional>#include <algorithm>#include <vector>#include <string>using namespace STD;using namespace STD::p laceholders;classperson{Private:stringname_; Public: Person (Const string& N): Name_ (n) {}voidPrint ()Const{cout<< name_ << Endl; }voidPrint2 (Const string& prefix)Const{cout<< prefix << name_ << Endl; }};intMain () { vector<Person>coll = {person ("CSU"), Person ("CSRU"), Person ("Csiu")    };//Each person object calls the member functionFor_each (Coll.begin (), Coll.end (), Bind (&person::p rint, _1));cout<< Endl; For_each (Coll.begin (), Coll.end (), Bind (&person::p rint2, _1,"Person:"));cout<< Endl; Bind (&person::p rint2, _1,"This is:") (Person ("Railway Academy")); System"Pause");}
/*运行结果csucsrucsiuPerson: csuPerson: csruPerson: csiuThis is : 铁道学院请按任意键继续. . .*/

Program Analysis:
A function object is defined in the program that bind(&Person::print, _1) calls Param1.print () for the incoming person,
That is, because the first argument is a member function, the next parameter defines the object to invoke the member function.
Any other arguments will be passed to the member function. This means that:

bind"Person: ")

Defines a function object that is invoked on the incoming person param1.print2("Person:") .

Lambda implementations call global functions and class member functions
//Global function#include <iostream>#include <algorithm>#include <locale>#include <string>using namespace STD;CharMy_toupper (Charc) {STD:: Locale loc;return STD::use_facet<STD::ctype<Char>> (Loc).ToUpper(c);}intMain () {stringS"Internationalizition");stringSub"Nation");string:: Iterator Pos; pos = Search (S.begin (), S.end (), Sub.begin (), Sub.end (), [] (CharC1,CharC2) {returnMy_toupper (C1) = = My_toupper (c2); }    );if(pos! = S.end ()) {cout<< Sub <<" is part of"<< s << Endl; } System ("Pause");}/*nation is part of internationalizition please press any key to continue ... * /
//class member function#include <iostream>#include <functional>#include <algorithm>#include <vector>#include <string>using namespace STD;using namespace STD::p laceholders;classperson{Private:stringname_; Public: Person (Const string& N): Name_ (n) {}voidPrint ()Const{cout<< name_ << Endl; }voidPrint2 (Const string& str)Const{cout<< str << name_ << Endl; }};intMain () { vector<Person>coll = {person ("CSU"), Person ("CSRU"), Person ("Csiu")    }; For_each (Coll.begin (), Coll.end (), [] (Constperson& p) {p.print (); });cout<< Endl; For_each (Coll.begin (), Coll.end (), [] (Constperson& p) {P.print2 ("Person:");    }); System"Pause");}/*csucsrucsiuperson:csuperson:csruperson:csiu Please press any key to continue ... * *

Bind () adapter (Adapter)--Call global function and member function

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.