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

 /*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. HTML5与CSS3权威指南.pdf8

    第17章 与背景和边框相关的样式 与背景相关的新增属性 background-clip指定背景的显示范围 background-origin指定绘制背景图像时的起点 background-size指定 ...

  2. Python win32打印示例

    # -*- coding:utf-8 -*- # Author: Pete Yim<xpHook@gmail.com> # Date : 13-8-22 # Copyright (c) 2 ...

  3. 如何实例化i2c_client(四法)

    一.在板文件进行client的实例化 在内核的初始化中(例如在板文件中)定义设备的信息.这种操作的前提是内核编译的时候已经确定有哪些i2c设备和它们的地址,还要知道连接的总线的编号. 比如在板文件/a ...

  4. 一起来说 Vim 语

    作为一款古老而具有持久生命力的编辑器,vim 自有它的强大之处.很多人觉得 Vim 的学习曲线太陡峭了,为了能够把 Vim 用得风生水起,不得不记忆大量的命令.如果你是 Vim 新手,刚入门就开始面对 ...

  5. MySQL数据库建立外键失败的原因总结

    在MySQL数据库创建外键时,经常会发生一些错误,这是一件很令人头疼的事.一个典型的错误就是:Can’t create table... 的错误.在很多实例中,这种错误的发生都是因为mysql一直以来 ...

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

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

  7. LCS算法

    转自:http://hzzy-010.blog.163.com/blog/static/79692381200872024242126/  好详细~~~也十分好理解~~~ 最长公共子序列问题(非连续的 ...

  8. Android学习路线图

    第一阶段:Java面向对象编程 1.Java基本数据类型与表达式,分支循环. 2.String和StringBuffer的使用.正则表达式. 3.面向对象的抽象,封装,继承,多态,类与对象,对象初始化 ...

  9. java转c#代码工具集合

    1#:Java语言转换器助手地址:http://www.microsoft.com/en-us/download/details.aspx?id=14349 2#:Octopus的.NET转换器地址: ...

  10. linux kernel (proc文件系统)参数

    http://blog.csdn.net/guowake/article/details/3279796 Linux Proc文件系统,通过对Proc文件系统进行调整,达到性能优化的目的. 二./pr ...