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 ...
随机推荐
- 《深入浅出WPF》 学习笔记
<深入浅出WPF> 序言 1. 什么是WPF 2. 为什么要学习WPF 第一章 XAML概览 1. XAML是什么? 2. XAML有哪些优点 第二章 从零起步认识XAML 1. 新 ...
- 安装ie10慢的解决办法
下面是win7安装ie10的先决条件: http://support.microsoft.com/kb/2818833
- markdownpad2使用说明
## 欢迎使用 MarkdownPad 2 ## **MarkdownPad** 是 Windows 平台上一个功能完善的 Markdown 编辑器. ### 专为 Markdown 打造 ### 提 ...
- RSA 加解密
#region RSA public static byte[] GetBytes(String num) { BigInteger n = ); String s = n.ToString(); & ...
- 十个最常见的Java字符串问题
翻译自:Top 10 questions of Java Strings 1.怎样比较字符串?用”==”还是用equals()? 简单地说,”==”测试两个字符串的引用是否相同,equals()测试两 ...
- linux系统环境变量.bash_profile/bashrc文件
系统环境变量的查看: [root@localhost ~]# envHOSTNAME=localhost.localdomainSELINUX_ROLE_REQUESTED=TERM=xtermSHE ...
- linux 编译内核[scripts/kconfig/dochecklxdialog] 错误
administrator@ubuntu:~/linux-2.6.28-omap$ make menuconfig *** Unable to find the ncurses libraries o ...
- LINQ to XML(1)
LINQ to XML可以两种方式和XML配合使用.第一种方式是作为简化的XML操作API,第二种方式是使用LINQ查询工具.下面我使用的是第二种方式. 主要内容:用LINQ查询语句对XML文件里的数 ...
- Dynamic - ExpandoObject学习心得
1. 今天下午在做开发过程中,遇到了一个问题,要往Xml文件中添加新的节点,做个xml开发的都知道该怎么做,这不是什么难事,我卡卡卡卡把这个问题解决了,但是新问题又来了,要对xml中对应的节点数据添 ...
- linux lsof命令的使用
lsof(list open files)是一个列出当前系统打开文件的工具.在UNIX环境下,任何事物都是以文件的形式存在的,通过文件不仅仅可以访问常规数据,还可以访问网络连接和硬件. 像传输协议(T ...