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

 /*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. Ubuntu 下安装Kibana和logstash

    原文地址:http://www.cnblogs.com/saintaxl/p/3946667.html 简单来讲他具体的工作流程就是 logstash agent 监控并过滤日志,将过滤后的日志内容发 ...

  2. 修复直接删除linux系统后grub丢失错误

    如果删除了系统后,grub丢失,开机出现“grub>”的话,可以用如下代码进入目标linux系统:grub>ls (hd0,X)/boot             //x为目标系统所在分区 ...

  3. mv、umask、chattr、lsattr命令

    mv命令行,即move 将文件移动到目录下 对文件或目录重命名 umask chattr 设置文件或目录的隐藏属性 lsattr显示文件或目录的隐藏属性 ls mv 1.txt aa ls cd aa ...

  4. hdoj 1827 Summer Holiday【强连通分量&&缩点】

    Summer Holiday Time Limit: 10000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  5. hdoj 1787 GCD Again【欧拉函数】

    GCD Again Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  6. poj 3468 A Simple Problem with Integers【线段树区间修改】

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 79137   ...

  7. linux下安装php的swoole扩展模块(安装后php加载不出来?)

    应开发同事要求,需要安装php的扩展模块swoole.swoole是一种PHP高级Web开发框架,框架不是为了提升网站的性能,而是为了提升网站的开发效率,以最少的性能损耗,换取最大的开发效率. 假设服 ...

  8. 前端学习-使用JS库Leaflet.js生成世界地图并获取标注地址经纬度。

    介绍:Leaflet是一个开源的JavaScript库,对移动端友好且对地图有很好的交互性. 大小仅仅只有 33 KB, 同时具有大多数地图所需要的特点. Leaflet设计的非常简单易懂, 同时具有 ...

  9. Zend Framework 2参考Zend\Authentication(HTTP认证适配器)

    Zend Framework 2参考Zend\Authentication(HTTP认证适配器) 介绍 Zend\Authentication\Adapter\Http提供了RFC-2617, Bas ...

  10. tomcat报错:This is very likely to create a memory leak问题解决

    tomcat memory leak解决方案 这种问题在开发中经常会碰到的,看看前辈的总结经验 Tomcat内存溢出的原因  在生产环境中tomcat内存设置不好很容易出现内存溢出.造成内存溢出是不一 ...