Example of boost: bind in four application scenarios

Source: Internet
Author: User

[Cpp] int f (int a, int B) {return a + B;} int g (int a, int B, int c) {return a + B + c ;} // function pointer defines typedef int (* f_type) (int, int); typedef int (* g_type) (int, int, int); // struct is only used for convenience, do not write public struct demo {int f (int a, int B) {return a + B ;}}; // function object struct sf {int operator () (int, int B) {return a + B ;}}; void test_bind_common () {std: cout <boost: bind (f, 1, 2) () <std:: Endl; std: cout <boost: bind (g, 1, 2, 3) () <std: endl;} // 1. boost: bind void test_bind_fun1 () {// bind (f, _ 1, 9) (x); // f (x, 9 ), equivalent to bind2nd (f, 9) // bind (f, _ 1, _ 2) (x, y); // f (x, y) // bind (f, _ 2, _ 1) (x, y); // f (y, x) // bind (f, _ 1, _ 1) (x, y ); // f (x, x), y parameter ignored // bind (g, _ 1, 8, _ 2) (x, y); // g (x, 8, y) // bind (g, _ 3, _ 2, _ 2) (x, y, z); // g (z, y, y ), the x parameter is ignored. int x = 1, y = 2, z = 3; std: cout <boost: bind (f, _ 1, 9) (x) <std: endl; std: cout <boost :: bind (f, _ 1, _ 2) (x, y) <std: endl; std: cout <boost: bind (f, _ 2, _ 1) (x, y) <std: endl; std: cout <boost: bind (f, _ 1, _ 1) (x, y) <std: endl; std: cout <boost: bind (g, _ 1, 8, _ 2) (x, y) <std: endl; std: cout <boost: bind (g, _ 3, _ 2, _ 2) (x, y, z) <std: endl ;} // 2. member function application boost: bind void test_bind_fun2 () {demo A, & ra = a; demo * p = & a; // The get address operator must be added before the member function &, it indicates that this is a member function pointer // The second parameter uses struct demo. struct demo * Can Be std: cout for both types <boost: bind (& demo: f, a, _ 1, 20) (10) <std: endl; std: cout <boost: bind (& demo: f, ra, _ 2, _ 1) (10, 20) <std: endl; std: cout <boost: bind (& demo: f, p, _ 1, _ 2) (10, 20) <std: endl;} // 3. member Variable void test_bind_val () {typedef std: pair <int, std: string> pair_t; pair_t p (123, "String"); std: cout <boost: bind (& pair_t: first, p) () <std: endl; std :: cout <boost: bind (& pair_t: second, p) () <std: endl ;}// 4. function object void test_bind_functor () {std: cout <boost: bind (std: plus <int> (), _ 1, _ 2) (10, 20) <std: endl; std: cout <boost: bind (std: modulus <int> (), _ 1, 3) (11) <std:: endl; std: cout <std: boolalpha <boost: bind (std: greater <int> (), _ 1, 10) (20) <std :: Endl; std: cout <boost: bind <int> (sf (), _ 1, _ 2) (11, 22) <std: endl ;} // you can use the ref library to wrap the object reference, so that the bind can store the copy of the object reference, thus reducing the copy cost. Void test_bind_ref () {// variable int x = 10; // 11 + 10 + 10 std: cout <boost: bind (g, _ 1, boost :: cref (x), boost: ref (x) (11) <std: endl; // a function object sf af; std: cout <boost :: bind <int> (boost: ref (af), _ 1, _ 2) (11, 22) <std: endl ;}

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.