第一部分源码为基础实践:

 /*Beyond the C++ Standard Library ( An Introduction to Boost )[CN].chm*/
/*bind的用法*/ #include <iostream>
#include <algorithm>
#include <functional>
#include <vector> #include <boost/bind/bind.hpp>
#include <boost/bind/apply.hpp>
#include <boost/smart_ptr/shared_ptr.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;
} bool preFind(int a, int b)
{
return a<b;
}
bool preFind2(int a, int b, int c)
{
return a<b && a<c;
}
struct predicate
{
typedef void result_type;
void operator() (int a, int b)
{
cout << a << "+" << b << "=" << a+b << endl;
}
};
struct X
{
void f(int a)
{
cout << "a = " << a << endl;
}
void f2(int a, int b)
{
cout << a << "+" << b << "=" << a+b << endl;
}
predicate pre;
int arrElem;
int *parrElem;
}; void main()
{
int arr[] = {, , , , };
//int ret1 = std::count_if(arr, arr+5, boost::bind(preFind, _1, 2));
//cout << ret1 << endl;
//int ret2 = std::count_if(arr, arr+5, boost::bind(preFind, 2, _2));
//cout << ret2 << endl;
//std::for_each (arr, arr+5, boost::bind(preFind, _1, 2));
//std::for_each (arr, arr+5, boost::bind(predicate(), _1, 2));
//std::for_each (arr, arr+5, boost::bind(boost::type<void>(), predicate(), _1, 2));
//std::for_each (arr, arr+5, boost::bind(preFind2, _1, 2, 3));
//std::for_each (arr, arr+5, boost::bind(preFind2, _1, 2, _1));
//boost::bind(preFind2, 5, _2, 3);
//std::for_each (arr, arr+5, boost::bind(preFind2, 5, _2, 3)); //boost::bind(preFind, _1, 2);
//boost::bind(preFind, _2, 2);
//boost::bind(preFind, _1, _2);
//boost::bind(preFind, _2, _1);
//boost::bind(preFind2, _1, _2, _3);
//boost::bind(predicate(), _1, _2);
//boost::bind(boost::type<void>(), predicate(), _1, _2); //X x;
//boost::bind(&X::f, boost::ref(x), _1);
//boost::bind(&X::f, _1, 1);
//boost::bind(&X::f, _1, _2);
//boost::bind(&X::pre, _1);
//std::for_each (arr, arr+5, boost::bind(&X::f, x, _1));
//std::for_each (arr, arr+5, boost::bind(&X::f2, boost::ref(x), _1, 2));
//std::for_each (arr, arr+5, boost::bind(&X::f2, &x, _1, 2));
//boost::shared_ptr<X> p(new X);
//std::for_each (arr, arr+5, boost::bind(&X::f2, p, _1, 2));
std::vector<X*> arrs;
for (int i= ; i!= ; ++i)
{
arrs.push_back(new X);
}
std::for_each (arrs.begin(), arrs.end(), boost::bind(&X::arrElem, _1));
std::for_each (arrs.begin(), arrs.end(), boost::bind(&X::f, _1, ));
}

第二部分源码为实际应用:

 /*Beyond the C++ Standard Library ( An Introduction to Boost )[CN].chm*/
/*bind的用法*/ #include <iostream>
#include <algorithm>
#include <functional>
#include <vector> #include <boost/bind/bind.hpp>
#include <boost/smart_ptr/shared_ptr.hpp> using namespace std;
using namespace boost; class status
{
public:
std::string name_;
bool ok_;
public:
status(const std::string& name):name_(name),ok_(true) {}
void break_it() { ok_=false; }
bool is_broken() const { return ok_; }
void report() const
{
std::cout << name_.c_str() << " is " << (ok_ ? "working nominally":"terribly broken") << std::endl;
}
}; void nine_arguments( int i1,int i2,int i3,int i4, int i5,int i6,int i7,int i8, int i9)
{
std::cout << i1 << i2 << i3 << i4 << i5 << i6 << i7 << i8 << i9 << '\n';
}
int main()
{
int i1=,i2=,i3=,i4=,i5=,i6=,i7=,i8=,i9=;
(boost::bind(&nine_arguments,_9,_2,_1,_6,_3,_8,_4,_5,_7))(i1,i2,i3,i4,i5,i6,i7,i8,i9); //std::vector<status> statuses;
//statuses.push_back(status("status 1"));
//statuses.push_back(status("status 2"));
//statuses.push_back(status("status 3"));
//statuses.push_back(status("status 4"));
//statuses[1].break_it();
//statuses[2].break_it();
////method 1:
//for (std::vector<status>::iterator it=statuses.begin(); it!=statuses.end();++it)
//{
// it->report();
//}
////method 2: ok
//std::for_each( statuses.begin(), statuses.end(), std::mem_fun_ref(&status::report));
////method 3: ok
//std::for_each( statuses.begin(), statuses.end(), boost::bind(&status::report, _1));
////others ok
//std::for_each( statuses.begin(), statuses.end(), boost::bind(&status::name_, _1)); //std::vector<status*> p_statuses;
//p_statuses.push_back(new status("status 1"));
//p_statuses.push_back(new status("status 2"));
//p_statuses.push_back(new status("status 3"));
//p_statuses.push_back(new status("status 4"));
//p_statuses[1]->break_it();
//p_statuses[2]->break_it();
////method 2: ok
//std::for_each( p_statuses.begin(), p_statuses.end(), std::mem_fun(&status::report));
////method 3: ok
//std::for_each( p_statuses.begin(), p_statuses.end(), boost::bind(&status::report, _1)); std::vector<boost::shared_ptr<status> > s_statuses;
s_statuses.push_back( boost::shared_ptr<status>(new status("status 1")));
s_statuses.push_back( boost::shared_ptr<status>(new status("status 2")));
s_statuses.push_back( boost::shared_ptr<status>(new status("status 3")));
s_statuses.push_back( boost::shared_ptr<status>(new status("status 4")));
s_statuses[]->break_it();
s_statuses[]->break_it();
//method 2: error
//std::for_each( s_statuses.begin(), s_statuses.end(), std::mem_fun(&status::report));
//method 3: ok
std::for_each( s_statuses.begin(), s_statuses.end(), boost::bind(&status::report, _1)); }

boost::bind实践的更多相关文章

  1. boost::bind实践2——来自《Beyond the C++ Standard Library ( An Introduction to Boost )》

    直接代码: 代码段1: #include <iostream> #include <string> #include <boost/bind/bind.hpp> c ...

  2. 1,Boost -> Bind

    #include <boost/bind.hpp> #include <boost/shared_ptr.hpp> #include <iostream> usin ...

  3. boost::bind

    bind并不是一个单独的类或函数,而是非常庞大的家族,依据绑定的参数个数和要绑定的调用对象类型,总共有十个不同的形式,但它们的名字都叫bind. bind接受的第一个参数必须是一个可调用对象f,包括函 ...

  4. boost::bind 和 boost::function 基本用法

    这是一篇介绍bind和function用法的文章,起因是近来读陈硕的文章,提到用bind和function替代继承,于是就熟悉了下bind和function的用法,都是一些网上都有的知识,记录一下,期 ...

  5. 以boost::function和boost:bind取代虚函数

    转自:http://blog.csdn.net/Solstice/archive/2008/10/13/3066268.aspx 这是一篇比较情绪化的blog,中心思想是“继承就像一条贼船,上去就下不 ...

  6. (转)boost::bind介绍

    转自:http://www.cnblogs.com/sld666666/archive/2010/12/14/1905980.html 这篇文章介绍boost::bind()的用法, 文章的主要内容是 ...

  7. boost::function实践——来自《Beyond the C++ Standard Library ( An Introduction to Boost )》

    代码段1: #include <boost/function.hpp> #include <iostream> float mul_ints(int x, int y) { r ...

  8. 关于boost::function与boost::bind函数的使用心得

    最近开始写一个线程池,期间想用一个通用的函数模板来使得各个线程执行不同的任务,找到了Boost库中的function函数. Boost::function是一个函数包装器,也即一个函数模板,可以用来代 ...

  9. [转] [翻译]图解boost::bind

    http://kelvinh.github.io/blog/2013/12/03/boost-bind-illustrated/ 其实这是很久之前留的一个坑了,一直没有填.. 记得在刚开始看到 boo ...

随机推荐

  1. jQuery技术内幕预览版.pdf2

    第二章 构造jQuery对象 jQuery对象是一个类数组对象,含有连续的整型属性.length属性和大量的jQuery方法,$()是jQuery()的缩写 构造函数jQuery() 如果调用构造函数 ...

  2. Yii PHP 框架分析(二)

    Yii PHP 框架分析(二)作者:wdy http://hi.baidu.com/delphiss/blog/item/54597af595085ad3f3d38552.html Yii是基于组件( ...

  3. linux下利用openssl来实现证书的颁发(详细步骤)

    1.首先需要安装openssl,一个开源的实现加解密和证书的专业系统.在centos下可以利用yum安装. 2.openssl的配置文件是openssl.cnf,我们一般就是用默认配置就可以.如果证书 ...

  4. Java多线程——Executors和线程池

    线程池的概念与Executors类的应用 1.创建固定大小的线程池 package java_thread; import java.util.concurrent.ExecutorService; ...

  5. Python抓取淘宝IP地址数据

    def fetch(ip): url = 'http://ip.taobao.com/service/getIpInfo.php?ip=' + ip result = [] try: response ...

  6. 非常实用的PHP常用函数汇总

    这篇文章主要介绍了非常实用的PHP常用函数,汇总了加密解密.字符串操作.文件操作.SQL注入等函数的实例与用法说明,在PHP项目开发中非常具有实用价值,需要的朋友可以参考下 本文实例总结了一些在php ...

  7. Quartz定时任务学习(五)触发器

    顾名思义,Trigger(触发器)的责任就是触发一个 Job 去执行.当用 Scheduler 注册一个 Job 的时候要创建一个 Trigger 与这个 Job 相关联.Quartz 提供了四种类型 ...

  8. 草稿-Hyper-V

    Hyper-V Over SMB3.0 为Hyper-v宿主机和故障转移群集做防病毒排除 微软SMB 3.0文件共享协议新特性介绍

  9. 修正android cocos2dx项目当点击属性时提示错误的问题

    近期在用cocos2dx 3.x版本号做android版本号的时候,出现点击project-属性-C/C++ builder的时候会提示 The currently displayed paye co ...

  10. AndroidUI组件之ActionBar--基于下拉的导航方式

      在上一篇关于ActionBar的博文中.我们知道了ActionBar是Android3.0的重要更新之中的一个.本篇博文就来写一个开发中经经常使用到的样例.用ActionBar提供基于下拉的导航方 ...