boost::bind

bind并不是一个单独的类或函数,而是非常庞大的家族,依据绑定的参数个数和要绑定的调用对象类型,总共有十个不同的形式,但它们的名字都叫bind.

bind接受的第一个参数必须是一个可调用对象f,包括函数,函数指针,函数对象和成员函数,之后bind接受最多9个参数,参数的数量必须与f的参数数量相等

_1,_2这些一直可以到9,是占位符,必须在绑定表达式中提供函数要求的所有参数,无论是真实参数还是占位符均可以。占位符不可以超过函数参数数量。

绑定普通函数:


1.#include<boost/bind.hpp>
2.#include<iostream>
3.using namespace std;
4.using namespace boost;
5.
6.void fun(int a,int b){
7. cout << a+b << endl;
8.}
9.
10.int main()
11.{
12. bind(fun,1,2)();//fun(1,2)
13. bind(fun,_1,_2)(1,2);//fun(1,2)
14. bind(fun,_2,_1)(1,2);//fun(2,1)
15. bind(fun,_2,_2)(1,2);//fun(2,2)
16. bind(fun,_1,3)(1);//fun(1,3)
17.}
18.
19.
20.3
21.3
22.3
23.4
24.4

绑定成员函数:


1.#include<boost/bind.hpp>
2.#include<iostream>
3.#include<vector>
4.#include<algorithm>
5.using namespace boost;
6.using namespace std;
7.
8.struct point
9.{
10. int x,y;
11. point(int a=0,int b=0):x(a),y(b){}
12. void print(){
13. cout << "(" << x << "," << y << ")\n";
14. }
15. void setX(int a){
16. cout << "setX:" << a << endl;
17. }
18. void setXY(int x,int y){
19. cout << "setX:" << x << ",setY:" << y << endl;
20. }
21. void setXYZ(int x,int y,int z){
22. cout << "setX:" << x << ",setY:" << y << "setZ:" << z << endl;
23. }
24.};
25.
26.int main()
27.{
28. point p1,p2;
29. bind(&point::setX,p1,_1)(10);
30. bind(&point::setXY,p1,_1,_2)(10,20);
31. bind(&point::setXYZ,p2,_1,_2,_3)(10,20,30);
32. vector<point> v(10);
33. //for_each的时候只需要_1就可以了
34. for_each(v.begin(),v.end(),bind(&point::print,_1));
35. for_each(v.begin(),v.end(),bind(&point::setX,_1,10));
36. for_each(v.begin(),v.end(),bind(&point::setXY,_1,10,20));
37. for_each(v.begin(),v.end(),bind(&point::setXYZ,_1,10,20,30));
38.}
39.
40.setX:10
41.setX:10,setY:20
42.setX:10,setY:20setZ:30
43.(0,0)
44.(0,0)
45.(0,0)
46.(0,0)
47.(0,0)
48.(0,0)
49.(0,0)
50.(0,0)
51.(0,0)
52.(0,0)
53.setX:10
54.setX:10
55.setX:10
56.setX:10
57.setX:10
58.setX:10
59.setX:10
60.setX:10
61.setX:10
62.setX:10
63.setX:10,setY:20
64.setX:10,setY:20
65.setX:10,setY:20
66.setX:10,setY:20
67.setX:10,setY:20
68.setX:10,setY:20
69.setX:10,setY:20
70.setX:10,setY:20
71.setX:10,setY:20
72.setX:10,setY:20
73.setX:10,setY:20setZ:30
74.setX:10,setY:20setZ:30
75.setX:10,setY:20setZ:30
76.setX:10,setY:20setZ:30
77.setX:10,setY:20setZ:30
78.setX:10,setY:20setZ:30
79.setX:10,setY:20setZ:30
80.setX:10,setY:20setZ:30
81.setX:10,setY:20setZ:30
82.setX:10,setY:20setZ:30

http://www.cnblogs.com/lzjsky/archive/2011/09/07/2169820.html

时间: 2024-12-06 13:12:57

boost::bind的相关文章

Boost::bind学习

1什么是函数对象 2Boostbind 绑定自由函数 绑定全部参数 不绑定全部参数 绑定类的函数 boostbind绑定的值是拷贝形式的值传递 1.什么是函数对象 在了解函数对象之前,应该先知道什么是函数指针,函数指针不再介绍了.函数对象用起来比函数指针更加灵活,且可以实现内联函数. 函数对象是具有函数功能的对象.怎么才能使对象具有函数的功能呢?答案是重载操作符().加入要实现两个整数相加: class Add { public: int operator()( int a, int b) {

Boost::Bind 基础

先了解一下:函数对象 重载函数调用操作符的类,其对象常称为函数对象(function object),即它们是行为类似函数的对象.[1] 一个类对象,表现出一个函数的特征,就是通过"对象名+(参数列表)"的方式使用一个 类对象,如果没有上下文,完全可以把它看作一个函数对待.这是通过重载类的 operator()来实现的.比如,对于调用 int s = sum(1, 1); 你可能把它看作一个函数调用: int sum(int i, int j) { return i+j; } 但很可能

boost::bind 详解

使用 boost::bind是标准库函数std::bind1st和std::bind2nd的一种泛化形式.其可以支持函数对象.函数.函数指针.成员函数指针,并且绑定任意参数到某个指定值上或者将输入参数传入任意位置. 1. 通过functions和function pointers使用bind 给定如下函数: 1 int f(int a, int b) 2 { 3 return a + b; 4 } 5 6 int g(int a, int b, int c) 7 { 8 return a + b

boost::bind boost::function

? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 #include <boost/bind.hpp> #include <boost/function.hpp> #include <iostream&g

boost::bind实践

第一部分源码为基础实践: 1 /*Beyond the C++ Standard Library ( An Introduction to Boost )[CN].chm*/ 2 /*bind的用法*/ 3 4 #include <iostream> 5 #include <algorithm> 6 #include <functional> 7 #include <vector> 8 9 #include <boost/bind/bind.hpp&g

1,Boost -&gt; Bind

#include <boost/bind.hpp> #include <boost/shared_ptr.hpp> #include <iostream> using namespace std; void dprint(int x,int y) { cout << x << " " <<y <<endl; } class Bind_test { public: void setData(int x,i

boost::bind测试代码

// Copyright (c) 2015 // Author: Chrono Law #include <std.hpp> //using namespace std; #include <boost/bind.hpp> using namespace boost; ////////////////////////////////////////// int f(int a, int b)<span style="white-space:pre">

boost::bind 介绍

boost::bind 介绍 这篇文章介绍boost::bind()的用法, 文章的主要内容是参考boost的文档. 1. 目的 boost::bind 是std::bindlist 和 std::bind2nd的结合体.它提供一个任意的函数对象(仿函数).函数.函数指针.成员函数指针. 它可以绑定任意的参数.bind 没有对函数对象有任何的要求. 2. 把bind()用在函数和函数指针上 有如下代码: 1 void f(int a, int b) 2 { 3 cout << a + b &l

(转)boost::bind介绍

转自:http://www.cnblogs.com/sld666666/archive/2010/12/14/1905980.html 这篇文章介绍boost::bind()的用法, 文章的主要内容是参考boost的文档. 1. 目的 boost::bind 是std::bindlist 和 std::bind2nd的结合体.它提供一个任意的函数对象(仿函数).函数.函数指针.成员函数指针. 它可以绑定任意的参数.bind 没有对函数对象有任何的要求. 2. 把bind()用在函数和函数指针上