bind并不是一个单独的类或函数,而是非常庞大的家族,依据绑定的参数个数和要绑定的调用对象类型,总共有十个不同的形式,但它们的名字都叫bind.
bind接受的第一个参数必须是一个可调用对象f,包括函数,函数指针,函数对象和成员函数,之后bind接受最多9个参数,参数的数量必须与f的参数数量相等
_1,_2这些一直可以到9,是占位符,必须在绑定表达式中提供函数要求的所有参数,无论是真实参数还是占位符均可以。占位符不可以超过函数参数数量。
绑定普通函数:

.#include<boost/bind.hpp>
.#include<iostream>
.using namespace std;
.using namespace boost;
.
.void fun(int a,int b){
. cout << a+b << endl;
.}
.
.int main()
.{
. bind(fun,,)();//fun(1,2)
. bind(fun,_1,_2)(,);//fun(1,2)
. bind(fun,_2,_1)(,);//fun(2,1)
. bind(fun,_2,_2)(,);//fun(2,2)
. bind(fun,_1,)();//fun(1,3)
.}
.
.
20.3
21.3
22.3
23.4
24.4 绑定成员函数:
.#include<boost/bind.hpp>
.#include<iostream>
.#include<vector>
.#include<algorithm>
.using namespace boost;
.using namespace std;
.
.struct point
.{
. int x,y;
. point(int a=,int b=):x(a),y(b){}
. void print(){
. cout << "(" << x << "," << y << ")\n";
. }
. void setX(int a){
. cout << "setX:" << a << endl;
. }
. void setXY(int x,int y){
. cout << "setX:" << x << ",setY:" << y << endl;
. }
. void setXYZ(int x,int y,int z){
. cout << "setX:" << x << ",setY:" << y << "setZ:" << z << endl;
. }
.};
.
.int main()
.{
. point p1,p2;
. bind(&point::setX,p1,_1)();
. bind(&point::setXY,p1,_1,_2)(,);
. bind(&point::setXYZ,p2,_1,_2,_3)(,,);
. vector<point> v();
. //for_each的时候只需要_1就可以了
. for_each(v.begin(),v.end(),bind(&point::print,_1));
. for_each(v.begin(),v.end(),bind(&point::setX,_1,));
. for_each(v.begin(),v.end(),bind(&point::setXY,_1,,));
. for_each(v.begin(),v.end(),bind(&point::setXYZ,_1,,,));
.}
.
.setX:
.setX:,setY:
.setX:,setY:20setZ:
.(,)
.(,)
.(,)
.(,)
.(,)
.(,)
.(,)
.(,)
.(,)
.(,)
.setX:
.setX:
.setX:
.setX:
.setX:
.setX:
.setX:
.setX:
.setX:
.setX:
.setX:,setY:
.setX:,setY:
.setX:,setY:
.setX:,setY:
.setX:,setY:
.setX:,setY:
.setX:,setY:
.setX:,setY:
.setX:,setY:
.setX:,setY:
.setX:,setY:20setZ:
.setX:,setY:20setZ:
.setX:,setY:20setZ:
.setX:,setY:20setZ:
.setX:,setY:20setZ:
.setX:,setY:20setZ:
.setX:,setY:20setZ:
.setX:,setY:20setZ:
.setX:,setY:20setZ:
.setX:,setY:20setZ:

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

boost::bind的更多相关文章

  1. 1,Boost -> Bind

    #include <boost/bind.hpp> #include <boost/shared_ptr.hpp> #include <iostream> usin ...

  2. boost::bind 和 boost::function 基本用法

    这是一篇介绍bind和function用法的文章,起因是近来读陈硕的文章,提到用bind和function替代继承,于是就熟悉了下bind和function的用法,都是一些网上都有的知识,记录一下,期 ...

  3. 以boost::function和boost:bind取代虚函数

    转自:http://blog.csdn.net/Solstice/archive/2008/10/13/3066268.aspx 这是一篇比较情绪化的blog,中心思想是“继承就像一条贼船,上去就下不 ...

  4. (转)boost::bind介绍

    转自:http://www.cnblogs.com/sld666666/archive/2010/12/14/1905980.html 这篇文章介绍boost::bind()的用法, 文章的主要内容是 ...

  5. boost::bind实践2——来自《Beyond the C++ Standard Library ( An Introduction to Boost )》

    直接代码: 代码段1: #include <iostream> #include <string> #include <boost/bind/bind.hpp> c ...

  6. boost::bind实践

    第一部分源码为基础实践: /*Beyond the C++ Standard Library ( An Introduction to Boost )[CN].chm*/ /*bind的用法*/ #i ...

  7. 关于boost::function与boost::bind函数的使用心得

    最近开始写一个线程池,期间想用一个通用的函数模板来使得各个线程执行不同的任务,找到了Boost库中的function函数. Boost::function是一个函数包装器,也即一个函数模板,可以用来代 ...

  8. [转] [翻译]图解boost::bind

    http://kelvinh.github.io/blog/2013/12/03/boost-bind-illustrated/ 其实这是很久之前留的一个坑了,一直没有填.. 记得在刚开始看到 boo ...

  9. 使用BOOST BIND库提高C++程序性能

    Boost.Bind为函数和函数对象,值语义和指针提供语义了一致的语法.我们首先通过一些简单的例子来看看它的基本用法,之后我们会延伸到嵌套绑定以实现功能组合.理解bind用法的一个关键是理解占位符(p ...

随机推荐

  1. Exchange之证书申请

          1.         打开EMC,选择服务器配置,然后右击新建证书   2.         输入一个好记的名称   3.         这里申请通配符证书   4.         填 ...

  2. 【学习笔记】Xcode常见设置

    一.设置主题和字体大小    二.设置显示代码行号

  3. 点击文字label同时选中checkbox radio

    在做网页的时候一般会有一个需求:点击一段文字信息的同时选中某个checkbox 旧处理方式是在这段文字上加上点击事件触发checkbox的选中事件 //jq中://选中 $("#ID&quo ...

  4. Oracle11g使用exp导出空表

    1.Oracle11g默认对空表不分配segment,故使用exp导出Oracle11g数据库时,空表不会导出. 2.设置deferred_segment_creation 参数为FALSE后,无论是 ...

  5. java基础-注解Annotation原理和用法

    在很多java代码中都可以看到诸如@Override.@Deprecated.@SuppressWarnings这样的字符,这些就是注解Annotation.注解最早在jdk5中被引入,现在已经成为j ...

  6. yum安装报错有冲突file /usr/lib64/php/modules/fileinfo.so conflicts between

    yum安装报错有冲突file /usr/lib64/php/modules/fileinfo.so conflicts between attempted installs of php-pecl-f ...

  7. lower_bound和upper_bound

    lower_bound:返回大于或等于val的第一个元素位置 upper_bound:返回大于val的第一个元素位置 两个函数用的都是二分查找

  8. 1014. Waiting in Line (30)

    Suppose a bank has N windows open for service. There is a yellow line in front of the windows which ...

  9. 关于字符串 “*****AB**C*D*****” 中前缀、后缀和中间 '*' 的处理

    一.删除前缀 '*' #include<iostream> #include<cstdio> using namespace std; //主函数 int main() { ] ...

  10. WPF-控件-ListView

    <Window x:Class="DataTemplate2.MainWindow" xmlns="http://schemas.microsoft.com/win ...