bind1st bind2nd在stl里面有具体的实现,只是只能绑定两个参数。

boost里面的bind使用bind(Fun f,A1 a1,A2,a2...)产生一个对象,这个对象可以有占位符,可以等到调用函数对象的时候传递参数进去即通过bind b(..);b(args);

bind绑定参数是通过存储值的形式,如果对象很大,需要耗费很大的代价,可以用ref,但是bind是延后处理的,需要保证所引用的对象在调用之前不被释放。

一.绑定普通函数

#include <iostream>
#include <string>
#include <boost/algorithm/string.hpp>
using namespace std;
using namespace boost; int f(int a,int b)
{
return a+ b;
} int g(int a,int b,int c)
{
return a + b *c;
} int main()
{
cout << bind(f,1,2)()<<endl;
cout << bind(g,1,2,3)() <<endl;
return 0;
}

二.绑定成员函数

由于成员函数需要对象才能调用,所以需要占用bind的一个参数位置。

#include <iostream>
#include <string>
#include <boost/algorithm/string.hpp>
#include <boost/bind.hpp>
using namespace std;
using namespace boost; struct demo
{
int f(int a,int b)
{
return a+ b;
}
}; int main()
{
demo a,&ra =a;
demo *p = &a;
cout << bind(&demo::f,a,_1,20)(10)<<endl;
return 0;
}

三.通过for_each迭代传递对象给bind,循环调用被适配的成员函数。

#include <iostream>
#include <string>
#include <vector>
#include <boost/algorithm/string.hpp>
#include <boost/bind.hpp>
using namespace std;
using namespace boost; struct demo
{
void print()
{
cout << "Hello"<<endl;
}
}; int main()
{ vector<demo> vec(10);
for_each(vec.begin(),vec.end(),boost::BOOST_BIND(&demo::print,_1));
return 0;
}

四.绑定成员变量,通过迭代向函数传递参数

#include <iostream>
#include <string>
#include <vector>
#include <boost/algorithm/string.hpp>
#include <boost/bind.hpp>
using namespace std;
using namespace boost; struct demo
{
void print()
{
cout << "Hello"<<endl;
}
int x;
}; void print(int x)
{
cout << x <<endl;
} int main()
{
demo de;
   de.x=357;
vector<demo> vec;
for (int i = 0;i < 10;++i)
{
vec.push_back(de);
} vector<int> vec2(10);
for_each(vec2.begin(),vec2.end(),bind(print,_1));
transform(vec.begin(),vec.end(),vec2.begin(),bind(&demo::x,_1));
for_each(vec2.begin(),vec2.end(),bind(print,_1));
return 0;
}

boost之bind的更多相关文章

  1. boost中bind的使用

    :first-child { margin-top: 0px; } .markdown-preview:not([data-use-github-style]) h1, .markdown-previ ...

  2. boost之bind,function,signal总结

    boost里的bind,function,signal三个组件都是对用函数做参数(其他算法也用函数做参数),对函数的某一项进行操作. bind主要是对函数参数的作用. function主要是对函数地址 ...

  3. boost库 bind/function的使用

    Boost::Function 是对函数指针的对象化封装,在概念上与广义上的回调函数类似.相对于函数指针,function除了使用自由函数,还可以使用函数对象,甚至是类的成员函数,这个就很强大了哈 # ...

  4. boost function bind ref

    boost::function to encapsulate function pointers. 1. function #include <boost/function.hpp> #i ...

  5. 基于boost的bind与function的一个简单示例消息处理框架

    前两年开始接触boost,boost库真是博大精深:今天简单介绍一下boost中之前用到的的bind与function,感觉挺实用的,分享给大家,我对boost用的也不多,让大家见笑了. 上次文发了一 ...

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

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

  7. boost bind及function的简单实现

    前面在做 http server 的时候,需要做一个回调的接口,要求能够绑定类的函数以及普通的函数到这个回调里,对于这种应用要求,选择 boost 的 bind 和 function 是最合适不过了, ...

  8. boost::spirit unicode 简用记录

    本文简单记录使用boost::spirit解析有中文关键字的字符串并执行响应动作,类似于语法分析+执行. 关键字:字符串解析 boost::spirit::qi::parse qi::unicode: ...

  9. C++ TR1 Function Bind

    在C++ 11出现以前,C++的事件一般是通过回调形试来实现,如 void (*func)(int,int,int),其实际上是一种函数指针,在C中调用时是直接写函数名在参数列表中,而在C++中,大部 ...

随机推荐

  1. http://www.shanghaihaocong.com-WORDPRESS开发的企业主题站

    wordpress是世界上使用最多的php开源博客系统,功能强大,而且拥有众多的插件,可扩展性强. 最近,我也用它做了一个企业网站,欢迎浏览:http://www.shanghaihaocong.co ...

  2. asp.net导出excel示例代码

    asp.net导出excel的简单方法. excel的操作,最常用的就是导出和导入. 本例使用NPOI实现. 代码:/// <summary> );             ;       ...

  3. Python脚本控制的WebDriver 常用操作 <十> 层级定位

    下面将使用WebDriver来模拟操作一个层级定位元素的操作 测试用例场景 在实际的项目测试中,经常会有这样的需求:页面上有很多个属性基本相同的元素,现在需要具体定位到其中的一个.由于属性基本相当,所 ...

  4. C#串口操作类,包括串口读写操作

    串口进行操作的类,其中包括写和读操作,类可设置串口参数.设置接收函数.打开串口资源.关闭串口资源,操作完成后,一定要关闭串口.接收串口数据事件.接收数据出错事件.获取当前全部串口.把字节型转换成十六进 ...

  5. Cygwin下软件安装 - apt-cyg

    安装了cygwin,但不能像centos上装yum,装东西很不方便.找了下可以用apt-cyg来安装软件. 1.下载apt-cyg $ wget raw.github.com/transcode-op ...

  6. Gearman 安装使用教程

    Gearman是一个分发任务的程序框架,可以用在各种场合,Gearman更偏向于任务分发功能.它的任务分布非常简单,简单得可以只需要用脚本即可完成. Gearman 分布式任务实现原理上只用到2个字段 ...

  7. SQL Server基础

    一.常用命令 1.使用命令行开启SQL Server服务 获取管理员权限的命令行工具: net start mssqlserver 开启sql server服务 net restart mssqlse ...

  8. android保存图片的方式

    Android中保存图片的两种方式 第一种是保存到数据库: 1.保存到数据库: 2.从数据库读取: 二是以图片格式保存到本地 1. A.声明tempFile以保存到指定路径: B.保存到tempFil ...

  9. hdu 5326 Work

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5326 Work Description It’s an interesting experience ...

  10. iOS中远程推送实现—在Apple的生产环境上测试Push Notifications功能

    1.在“Provisioning Profiles”中点击“Add”按钮. 2.在“What type of provisioning profile do you need?”页面中选择“Distr ...