boost::bind
bind并不是一个单独的类或函数,而是非常庞大的家族,依据绑定的参数个数和要绑定的调用对象类型,总共有十个不同的形式,但它们的名字都叫bind.
bind接受的第一个参数必须是一个可调用对象f,包括函数,函数指针,函数对象和成员函数,之后bind接受最多9个参数,参数的数量必须与f的参数数量相等
_1,_2这些一直可以到9,是占位符,必须在绑定表达式中提供函数要求的所有参数,无论是真实参数还是占位符均可以。占位符不可以超过函数参数数量。
绑定普通函数:
.#include<boost/bind.hpp>
.#include<iostream>
.using namespace std;
.using namespace boost;
.
.void fun(int a,int b){
. cout << a+b << endl;
.}
.
.int main()
.{
. bind(fun,,)();//fun(1,2)
. bind(fun,_1,_2)(,);//fun(1,2)
. bind(fun,_2,_1)(,);//fun(2,1)
. bind(fun,_2,_2)(,);//fun(2,2)
. bind(fun,_1,)();//fun(1,3)
.}
.
.
20.3
21.3
22.3
23.4
24.4 绑定成员函数:
.#include<boost/bind.hpp>
.#include<iostream>
.#include<vector>
.#include<algorithm>
.using namespace boost;
.using namespace std;
.
.struct point
.{
. int x,y;
. point(int a=,int b=):x(a),y(b){}
. void print(){
. cout << "(" << x << "," << y << ")\n";
. }
. void setX(int a){
. cout << "setX:" << a << endl;
. }
. void setXY(int x,int y){
. cout << "setX:" << x << ",setY:" << y << endl;
. }
. void setXYZ(int x,int y,int z){
. cout << "setX:" << x << ",setY:" << y << "setZ:" << z << endl;
. }
.};
.
.int main()
.{
. point p1,p2;
. bind(&point::setX,p1,_1)();
. bind(&point::setXY,p1,_1,_2)(,);
. bind(&point::setXYZ,p2,_1,_2,_3)(,,);
. vector<point> v();
. //for_each的时候只需要_1就可以了
. for_each(v.begin(),v.end(),bind(&point::print,_1));
. for_each(v.begin(),v.end(),bind(&point::setX,_1,));
. for_each(v.begin(),v.end(),bind(&point::setXY,_1,,));
. for_each(v.begin(),v.end(),bind(&point::setXYZ,_1,,,));
.}
.
.setX:
.setX:,setY:
.setX:,setY:20setZ:
.(,)
.(,)
.(,)
.(,)
.(,)
.(,)
.(,)
.(,)
.(,)
.(,)
.setX:
.setX:
.setX:
.setX:
.setX:
.setX:
.setX:
.setX:
.setX:
.setX:
.setX:,setY:
.setX:,setY:
.setX:,setY:
.setX:,setY:
.setX:,setY:
.setX:,setY:
.setX:,setY:
.setX:,setY:
.setX:,setY:
.setX:,setY:
.setX:,setY:20setZ:
.setX:,setY:20setZ:
.setX:,setY:20setZ:
.setX:,setY:20setZ:
.setX:,setY:20setZ:
.setX:,setY:20setZ:
.setX:,setY:20setZ:
.setX:,setY:20setZ:
.setX:,setY:20setZ:
.setX:,setY:20setZ:
http://www.cnblogs.com/lzjsky/archive/2011/09/07/2169820.html
boost::bind的更多相关文章
- 1,Boost -> Bind
#include <boost/bind.hpp> #include <boost/shared_ptr.hpp> #include <iostream> usin ...
- boost::bind 和 boost::function 基本用法
这是一篇介绍bind和function用法的文章,起因是近来读陈硕的文章,提到用bind和function替代继承,于是就熟悉了下bind和function的用法,都是一些网上都有的知识,记录一下,期 ...
- 以boost::function和boost:bind取代虚函数
转自:http://blog.csdn.net/Solstice/archive/2008/10/13/3066268.aspx 这是一篇比较情绪化的blog,中心思想是“继承就像一条贼船,上去就下不 ...
- (转)boost::bind介绍
转自:http://www.cnblogs.com/sld666666/archive/2010/12/14/1905980.html 这篇文章介绍boost::bind()的用法, 文章的主要内容是 ...
- 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实践
第一部分源码为基础实践: /*Beyond the C++ Standard Library ( An Introduction to Boost )[CN].chm*/ /*bind的用法*/ #i ...
- 关于boost::function与boost::bind函数的使用心得
最近开始写一个线程池,期间想用一个通用的函数模板来使得各个线程执行不同的任务,找到了Boost库中的function函数. Boost::function是一个函数包装器,也即一个函数模板,可以用来代 ...
- [转] [翻译]图解boost::bind
http://kelvinh.github.io/blog/2013/12/03/boost-bind-illustrated/ 其实这是很久之前留的一个坑了,一直没有填.. 记得在刚开始看到 boo ...
- 使用BOOST BIND库提高C++程序性能
Boost.Bind为函数和函数对象,值语义和指针提供语义了一致的语法.我们首先通过一些简单的例子来看看它的基本用法,之后我们会延伸到嵌套绑定以实现功能组合.理解bind用法的一个关键是理解占位符(p ...
随机推荐
- 更改Activity亮度
有些需求需进入到页面后更改Activity的亮度,退出页面后恢复到之前的亮度.通过更改WindowManager.LayoutParams的screenBrightness可以达到这个效果.scree ...
- 二十、Android -- SDcard文件读取和保存
背景 一些东西可以 ...
- JS验证框架(exValidation)
exValidation是一个前台校验框架 能够校验前台的常用的输入错误. 例如,必须输入,用户输入长度...... ----------------------------------------- ...
- 使用jquery插件报错:TypeError:$.browser is undefined的解决方法
关于$.browser browser就是用来获取浏览器基本信息的. jQuery 从 1.9 版开始,移除了 $.browser 和 $.browser.version , 取而代之的是 $.sup ...
- 导航栏 UITabBarController等颜色的区别
//tint color是设置你选中的那个tabBar的颜色,默认是蓝色,点击是设置的红色 vc.tabBar.tintColor = [UIColor redColor]; // ...
- POJ 2533
最长上升子序列裸题在网上看到有两种方法...一种复杂度O(N^2),一种O(NlogN).orz O(N^2): #include<cstdio> #define N 1001 int m ...
- JS判断客户端是手机还是PC
function IsPC() { var userAgentInfo = navigator.userAgent; var Agents = ["Android", " ...
- EasyUI_Datagrid学习总结
EasyUI_Datagrid学习总结 2016年7月25日星期一 一.简介 Easyui中的datagrid从总的作用上讲,就是在列表上显示数据,类似于table,但是在table的基础上,此控件更 ...
- RGB颜色二值化
原理:RGB颜色根据计算'灰度'的公式,可以转化为黑白2种颜色,实现二值化. 业务场景的应用:可以根据背景颜色,取一个黑色或白色的颜色,作为背景色上的文案字体颜色 具体代码: function get ...
- 基于PinnedSectionListView实现联系人通讯录并且点击打电话
PinnedSectionListView具体下载地址.使用方法和注意事项:http://www.cnblogs.com/zzw1994/p/4997601.html 怎么根据联系人姓名首字符顺序读取 ...