C + + one bind

Source: Internet
Author: User



Bind is a set of templates for function bindings. When you bind a function, you can specify some or all of the parameters, you can specify no parameters, and you can adjust the order of each parameter. For unspecified parameters, you can use the placeholder _1, _2, and _3 to represent them. 1 represents the 1th parameter of the bound function, _2 represents the 2nd parameter of the bound function, and so on.

Bind can be bound to normal functions, function objects, member functions of a class, and member variables of a class. The following are described separately.

1. Common functions


1 void nine_arguments (int i1,int i2,int i3,int i4,int i5,int i6,int i7,int i8,int I9);
2 int i1=1, i2=2, i3=3, i4=4, i5=5, i6=6, i7=7, i8=8, i9=9;
3 Bind (Nine_arguments,_9,_2,_1,_6,_3,_8,_4,_5,_7 (I1,I2,I3,I4,I5,I6,I7,I8,I9);
4 bind (Nine_arguments,i9,i2,i1,i6,i3,i8,_1,_2,_1) (I8,I9);
5 bind (Nine_arguments, I9,i2,i1,i6,i3,i8,i4,i5,i7) ();
2. Function Object


1 class Cstudent
2 {
3 Public:
4 void Operator () (string strName, int nAge)
5 {
6 cout << strName << ":" << nAge << Endl;
7}
8};
9 Bind (Cstudent (), "Mike", _1) (12);


3. member functions of a class


1 struct TADD
2 {
3 int Add (int x,int y)
4 {
5 return x+y;
6}
7};
8 TAdd TAdd;
9 TAdd *p = new TAdd ();
Ten shared_ptr<tadd> *q (p);
Bind (Tadd::add, TADD, 2, 3) ();
Bind (Tadd::add, p, 2, 3) ();
Bind (Tadd::add, Q, 2, 3) ();


4. member variables of the class


1 void Output (const string &name)
2 {
3 cout << name << Endl;
4}
5
6 Map<int, string> map1;
7 For_each (Map1.begin (), Map1.end (), bind (Output, bind (Map<int,
8 String>::value_type::second, _1)));


Bind can also be nested. Suppose you have a CPerson class that has an interface int getage () that gets the age, and now has a vector of CPerson objects that needs to be sorted, you can use bind as follows:

1 vector<cperson> Vctperson;
2 sort (Vctperson.begin (), Vctperson.end (), Bind (Less<int> (),
3 bind (Cperson::getage, _1), bind (Cperson::getage, _2));
Suppose you have a vector of integers, and now want to get the number of integers that are greater than 20 but less than 30, then there are:

1 count_if (Vctnum.begin (), Vctnum.end, Bind (logic_and<bool> (),
2 bind (Greater<int> (), _1, Bind (Less<int> (), _1, 30));
There are also some areas that require special attention when using bind, as described below.
1. For parameters that specify a value, the function object returned by bind saves the values and is saved by default. Consider the following code:


1 void Inc (int &a) {a++;}
2 int n = 0;
3 Bind (inc, N) ();
After calling the function object returned by BIND, n is still equal to 0. This is due to bind, when a copy of n is passed in. If you need to pass in a reference to n, you can use the ref or CREF function, such as:

1 Bind (inc, ref (n)) (); n is now equal to 1.
2. The first parameter of BIND is a function object and cannot be replaced with a placeholder. Consider the following code:

1 typedef function<void (int) > Func;
2 vector<func> Vctfunc;
3 For_each (Vctfunc.begin (), Vctfunc.end (), Bind (_1, 5)); Compilation error
At this point, you can use the Apply template. The first parameter of the Apply template is the passed-in function object, which can be followed by several parameters that represent the parameters of the function object. Like what:

1 apply<void> A; Void is the return value type of a function object
2 A (f); Equivalent to calling F ()
3 A (f, X); Equivalent to calling F (x)
4 A (f, x, y); Equivalent to calling f (x, Y)
With apply, we can pass the elements in Vctfunc as placeholders. The reference code is as follows:

1 For_each (Vctfunc.begin (), Vctfunc.end (), Bind (Apply<void> (), _1, 5));



Reprint http://www.cnblogs.com/hujian/archive/2012/12/08/2809298.html

If you have copyright issues, please contact QQ 858668791

C + + one bind

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.