boost::bind实践
第一部分源码为基础实践:
/*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实践的更多相关文章
- boost::bind实践2——来自《Beyond the C++ Standard Library ( An Introduction to Boost )》
直接代码: 代码段1: #include <iostream> #include <string> #include <boost/bind/bind.hpp> c ...
- 1,Boost -> Bind
#include <boost/bind.hpp> #include <boost/shared_ptr.hpp> #include <iostream> usin ...
- boost::bind
bind并不是一个单独的类或函数,而是非常庞大的家族,依据绑定的参数个数和要绑定的调用对象类型,总共有十个不同的形式,但它们的名字都叫bind. bind接受的第一个参数必须是一个可调用对象f,包括函 ...
- 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::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 ...
- 关于boost::function与boost::bind函数的使用心得
最近开始写一个线程池,期间想用一个通用的函数模板来使得各个线程执行不同的任务,找到了Boost库中的function函数. Boost::function是一个函数包装器,也即一个函数模板,可以用来代 ...
- [转] [翻译]图解boost::bind
http://kelvinh.github.io/blog/2013/12/03/boost-bind-illustrated/ 其实这是很久之前留的一个坑了,一直没有填.. 记得在刚开始看到 boo ...
随机推荐
- 如何解决PHP生成UTF-8编码的CSV文件用Excel打开乱码的问题
为了识别 Unicode 文件,Microsoft 建议所有的 Unicode 文件应该以 ZERO WIDTH NOBREAK SPACE字符开头.这作为一个”特征符”或”字节顺序标记(byte-o ...
- tomcat详细日志配置
在server.xml里的<host>标签下加上<Valve className="org.apache.catalina.valves.AccessLogValve&qu ...
- OpenSUSE SuSEfirewall2
1,修改SuSEfirewall2配置文件放行相应的端口方法vim /etc/sysconfig/SuSEfirewall2#TCP端口的情况:FW_SERVICES_EXT_TCP ="2 ...
- 控制反转(IoC)与依赖注入(DI)
前言 最近在学习Spring框架,它的核心就是IoC容器.要掌握Spring框架,就必须要理解控制反转的思想以及依赖注入的实现方式.下面,我们将围绕下面几个问题来探讨控制反转与依赖注入的关系以及在Sp ...
- 1 weekend110的hdfs源码跟踪之打开输入流 + hdfs源码跟踪之打开输入流总结
3种形式的元数据,fsimage是在磁盘上,meta.data是在内存上, 我们继续,前面呢,断点是打在这一行代码处, FileSystem fs = FileSystem.get(conf); we ...
- python的import与from...import的不同之处
在python用import或者from...import来导入相应的模块.模块其实就是一些函数和类的集合文件,它能实现一些相应的功能,当我们需要使用这些功能的时候,直接把相应的模块导入到我们的程序中 ...
- Mysql Binlog 三种格式介绍及分析
一.Mysql Binlog格式介绍 Mysql binlog日志有三种格式,分别为Statement,MiXED,以及ROW! 1.Statement:每一条会修改数据的sql都会记录在 ...
- Unity3D跨平台时partial分部方法的使用
最近看到项目中插件的一部分逻辑,发现问题多多,可读性很差,并且容易出错,于是随手整理了下逻 辑.Unity3D的插件逻辑,因为要考虑到针对各平台的移植,因此会大片的出现#if/#endif等条件编译, ...
- SQL SERVER 查询死锁
USE mastergo CREATE PROCEDURE [dbo].[sp_who_lock]AS BEGIN DECLARE @spid INT , ...
- MobilePhone正则表达式
电话号码正则表达式(支持手机号码,3-4位区号,7-8位直播号码,1-4位分机号) ((\d{11})|^((\d{7,8})|(\d{4}|\d{3})-(\d{7,8})|(\d{4}|\d{3} ...