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. datagridview下拉框

    下面介绍Winform中DataGridView的DataGridViewComboBoxColumn的使用方法: //首先获取数据源 //自己建立的静态数据源,你也可以从数据库读取 DataTabl ...

  2. ASP.NET的错误处理机制之一(概念)

    对Web应用程序来说,发生不可预知的错误和异常在所难免,我们必须为Web程序提供错误处理机制.当错误发生时,我们必须做好两件事情:一是将错误信息记录日志,发邮件通知网站维护人员,方便技术人员对错误进行 ...

  3. php设计模式之Proxy(代理模式)和Facade(外观)设计模式

    Proxy(代理模式)和Facade(外观)设计模式它们均为更复杂的功能提供抽象化的概念,但这两种实现抽象化的过程大不相同 Proxy案例中,所有的方法和成员变量都来自于目标对象,必要时,该代理能够对 ...

  4. 使用 Attribute +反射 来对两个类之间动态赋值

    看同事使用的 一个ORM 框架 中 有这样一个功能  通过特性(附加属性)的功能来 实现的两个类对象之间动态赋值的 功能 觉得这个功能不错,但是同事使用的 ORM 并不是我使用的  Dapper  所 ...

  5. python内建函数-数字相关

    本篇对于数字有关的内置函数进行总结. 数字包括 int() , long() , float() , complex() ,这些函数都能够用来进行数值类型的转换.同时这些函数也接受字符串参数,返回字符 ...

  6. 5.python的字符串

    在前面提起过字符串这个词,现在就来学习什么是字符串. 首先,字符串是python内置的数据类型,其特点是用引号引起来,并且可以是使用单引号('字符串'),双引号("字符串"),三个 ...

  7. JavaWeb之Servlet: ServletConfig 与 ServletContext

    ServletConfig对象 什么是ServletConfig对象 ServletConfig对象,叫Servlet配置对象.主要用于加载配置文件的初始化参数. 创建时机 ServletConfig ...

  8. 263. Ugly Number

    Write a program to check whether a given number is an ugly number. Ugly numbers are positive numbers ...

  9. arcgis中使用excel中x,y坐标创建点问题

    文件——从x,y中添加,可以显示点的位置 右击图层导出数据时,出现无法绘制图形,生成shapefile文件的情况.经过排除数据发现 当x,y坐标值中出现null等异常值时,会出现上述无法导出的情况.

  10. Sql Server数据的加密与解密

    Sql Server数据的加密与解密 在sql server中,我们如何为数据进行加密与解密,避免使用者窃取机密数据? 对于一些敏感数据,如密码.卡号,一般不能使用正常数值来存储.否则会有安全隐患.以 ...