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++中,大部 ...
随机推荐
- 遇到Wampserver遇到的问题
- MVC5 Identity 用用户名登录而不用电子邮件
1.修改AccountViewModels ·修改RegisterViewModel public class RegisterViewModel { [Required] [Display(Name ...
- [leetcode]_Merge Two Sorted Lists
题目:合并两个有序单链表 思路:一开始想复杂了,以为必须在原链表上修改(绕来绕去还AC了,但是思路相当绕),其实没有,按照正常地合并两个数组同样的方法也对. 代码: public ListNode m ...
- BF算法和KMP算法(javascript版本)
var str="abcbababcbababcbababcabcbaba";//主串 var ts="bcabcbaba";//子串 function BF( ...
- grep命令实战
显示/etc/rc.d/rc.sysinit中以#开头,且后面跟一个或多个空白字符,而后又跟了任意非空白字符的行 grep "^#[[:space:]]\+.\+" /etc/rc ...
- 一个伪ajax图片上传代码的例子
一个伪ajax图片上传实现代码. 复制代码代码如下: <?php if($_FILES){ ?> <script> window.parent.ajaxUploadPi ...
- php文件上传的例子
1.上传表单 upload.html 程序代码 [html] view plaincopy <form enctype="multipart/form-data" actio ...
- css 串联选择器和后代选择器
串联选择器:作用在同一个标签上 <div class=”a” id ="qq"><span>look at the color</span>&l ...
- static in C/C++
最近经常碰到static,之前也使用过,但都是一知半解,所以下决心做个整理总结,搞搞灵清它到底用哪些作用. 一.static in C 1.默认初始化为0: 如果不显式地对静态变量进行初始化,它们将被 ...
- STM32F0xx_DMA收发USART数据配置详细过程
前言 关于DMA(Direct Memory Access)的功能,前面关注我微信的人应该知道,其实我已经在F1芯片上简单讲了一下.有网友要求在F0讲解一下使用DMA收发串口数据.今天就应网友要求总结 ...