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. 转载:T-SQL语句大全

    一.基础 1.创建数据库 CREATE DATABASE database-name 2.删除数据库 drop database dbname 3.备份数据库 --- 创建 备份数据的 device ...

  2. css中li、a、span行内强制不换行

    li.a.span行内强制不换行:white-space:nowrap; 没有之前的效果 加上white-space:nowrap;后

  3. AppSetting ,connectionStrings配置节

    <appSettings> <!-- 当前使用的学校代码 --> <add key="DefaultCompanyID" value="cs ...

  4. centos6.7下编译安装lnmp

    很多步骤不说明了,请参照本人的centos6.7下编译安装lamp,这次的架构是nginx+php-fpm一台服务器,mysql一台服务器 (1)首先编译安装nginx: 操作命令: yum -y g ...

  5. mariadb主从复制架构学习笔记

    复制功用: 数据分布 负载均衡:读操作,适用于读密集型的应用 备份 高可用和故障切换 MySQL升级测试 在从服务器上有两个线程: I/O线程:从master请求二进制日志信息,并保存至中继日志 SQ ...

  6. js数组的内部实现,迭代器,生成器和内包

    js内部实现 在js以外的很多语言中,数组将会隐式占用一段连续的内存空间.这种隐式的内部实现,使得高效的内存使用及高速的元素方法称为可能,而 在javascript中,数组实体是一个对象,所以通常的实 ...

  7. SQLServer2005中查询语句的执行顺序

    SQLServer2005中查询语句的执行顺序   --1.from--2.on--3.outer(join)--4.where--5.group by--6.cube|rollup--7.havin ...

  8. linux内核SPI总线驱动分析(一)(转)

    linux内核SPI总线驱动分析(一)(转) 下面有两个大的模块: 一个是SPI总线驱动的分析            (研究了具体实现的过程) 另一个是SPI总线驱动的编写(不用研究具体的实现过程) ...

  9. 第九章 管理类型(In .net4.5) 之 继承机制

    1. 概述 本章包括 设计和实现接口.创建和使用基类 以及 使用.net类库提供的标准接口. 2. 主要内容 2.1 设计和实现接口 一个接口包含公共的方法.属性.事件.索引器.类和结构都可以实现接口 ...

  10. C 网页压力测试器

    引言 <<独白>> 席慕蓉 节选一 把向你借来的笔还给你吧. 一切都发生在回首的刹那. 我的彻悟如果是缘自一种迷乱,那么,我的种种迷乱不也就只是因为一种彻悟? 在一回首间,才忽 ...