boost之bind
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的更多相关文章
- boost中bind的使用
:first-child { margin-top: 0px; } .markdown-preview:not([data-use-github-style]) h1, .markdown-previ ...
- boost之bind,function,signal总结
boost里的bind,function,signal三个组件都是对用函数做参数(其他算法也用函数做参数),对函数的某一项进行操作. bind主要是对函数参数的作用. function主要是对函数地址 ...
- boost库 bind/function的使用
Boost::Function 是对函数指针的对象化封装,在概念上与广义上的回调函数类似.相对于函数指针,function除了使用自由函数,还可以使用函数对象,甚至是类的成员函数,这个就很强大了哈 # ...
- boost function bind ref
boost::function to encapsulate function pointers. 1. function #include <boost/function.hpp> #i ...
- 基于boost的bind与function的一个简单示例消息处理框架
前两年开始接触boost,boost库真是博大精深:今天简单介绍一下boost中之前用到的的bind与function,感觉挺实用的,分享给大家,我对boost用的也不多,让大家见笑了. 上次文发了一 ...
- boost::bind实践2——来自《Beyond the C++ Standard Library ( An Introduction to Boost )》
直接代码: 代码段1: #include <iostream> #include <string> #include <boost/bind/bind.hpp> c ...
- boost bind及function的简单实现
前面在做 http server 的时候,需要做一个回调的接口,要求能够绑定类的函数以及普通的函数到这个回调里,对于这种应用要求,选择 boost 的 bind 和 function 是最合适不过了, ...
- boost::spirit unicode 简用记录
本文简单记录使用boost::spirit解析有中文关键字的字符串并执行响应动作,类似于语法分析+执行. 关键字:字符串解析 boost::spirit::qi::parse qi::unicode: ...
- C++ TR1 Function Bind
在C++ 11出现以前,C++的事件一般是通过回调形试来实现,如 void (*func)(int,int,int),其实际上是一种函数指针,在C中调用时是直接写函数名在参数列表中,而在C++中,大部 ...
随机推荐
- T—SQL用法剪辑,方便以后查看
一.用T-SQL查询表中第n行到第m行数据的写法示例 假设这里的n=6,m=10则有以下两种写法,qusID可以不连续,如下: select top 5 * from tb_wenti where q ...
- C#中sizeof的用法实例分析
这篇文章主要介绍了C#中sizeof的用法,包括了常见的用法及注释事项,需要的朋友可以参考下. sizeof是C#中非常重要的方法,本文就以实例形式分析C#中sizeof的用法.分享给大家供大家参 ...
- js 实现栈
function Stack() { this.dataStore = []; this.top = 0; this.push=push; this.pop=pop; this.peek=peek; ...
- 删:[CentOS 7] 安装nginx
下载对应当前系统版本的nginx包(package) # wget http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-cent ...
- sliding windows (poj 2823) 题解
[问题描述] 给你一个长度为N的数组,一个长为K的滑动的窗体从最左移至最右端,你只能见到窗口的K个数,每次窗体向右移动一位,如下表: [样例输入] 8 3 1 3 -1 -3 5 3 6 7 [样例输 ...
- How to using x++ code create GL journal[AX2012]
static void FAN_GLImport(Args _args) { AxLedgerJournalTable header = new AxLedgerJournalTable(); AxL ...
- [.ashx檔?泛型处理例程?]基础入门#3....ADO.NET 与 将DB里面的二进制图片还原
原文出處 http://www.dotblogs.com.tw/mis2000lab/archive/2013/08/20/ashx_beginner_03_db_picture_show.aspx ...
- 关于HTML中,绝对定位,相对定位的理解...(学习HTML过程中的小记录)
关于HTML中,绝对定位,相对定位的理解...(学习HTML过程中的小记录) 作者:王可利(Star·星星) HTML中 相对定位:position:relative; 绝对定位:position ...
- C++类实现三维数组算法
在学习北京大学教授的<程序设计实习 / Practice on Programming>中,遇到了一个习题,花了很长时间研究,现在分享出来: 课题地址:https://class.cour ...
- android开发系列之git常用命令
最近因为跳槽到新公司,然后新公司里面的代码管理工具是gitLab,所以我想在这篇博客里面整理一下git常用的语法. GitLab是利用 Ruby on Rails 一个开源的版本管理系统,实现一个自托 ...