boost::bind 和 boost::function 基本用法
这是一篇介绍bind和function用法的文章,起因是近来读陈硕的文章,提到用bind和function替代继承,于是就熟悉了下bind和function的用法,都是一些网上都有的知识,记录一下,期冀对他人也有用处。
注:本文暂时不探索bind和function的实现和开销。
1. bind 是什么
boost::bind 是std::bindlist 和 std::bind2nd的结合体。它提供一个任意的函数对象(仿函数)、函数、函数指针、成员函数指针。 它可以绑定任意的参数。bind 没有对函数对象有任何的要求。
2. bind 到函数/函数指针
void print(int array[], int size)
{
for (int i = ; i < size; i++) {
printf("%d\n", array[i]);
}
} int main(int argc, const char *argv[])
{
int array[]= {,,,,,,,,,};
int len = sizeof(array)/sizeof(array[]);
// 绑定普通函数 boost::bind(print, _1,_2)(array, len);
// 与function结合 boost::function<void(int [], int)> fn = boost::bind(print, _1,_2);
fn(array, len);
return ;
}
注意,_1 和_2 表示占位符。
3. bind到类成员函数
class Thread {p
public:
Thread (int id):thread_id(id)
{}
virtual void Run()
{
printf("this is threads %d\n", thread_id);
}
private:
int thread_id;
};
class CallWrapper {
public:
typedef boost::function<void ()> CallBack;
CallWrapper ();
static void Run(CallBack fn)
{
printf("%s\t%d\n", __FUNCTION__, __LINE__);
fn();
}
};
int main()
{
Thread worker();
// 绑定类成员函数,worker相当于this指针
boost::bind(&Thread::Run, worker)();
// 作为参数,参数用function
CallWrapper::Run(boost::bind(&Thread::Run, worker));
}
上面是一个比较简单的市立,下面给出一个比较适用的分行读取文件的例子
#include <boost/function.hpp>
#include <boost/bind.hpp>
#include <stdio.h>
#include <string>
#include <map> class SimpleLineReader {
public:
SimpleLineReader (const string& filename)
{
fp_ = fopen(filename.c_str(), "r");
}
~SimpleLineReader (){
if (fp_) {
fclose(fp_);
}
}
typedef boost::function<bool (const std::string& line)> Callback;
bool ProcessLines(Callback fn)
{
if (!fp_)
{
return false;
}
char *line = NULL;
size_t len = ;
ssize_t read;
while ((read = getline(&line, &len, fp_)) != -) {
string str(line, read);
fn(line);
}
free(line);
return true;
}
private:
FILE* fp_;
}; class CityLoader {
public:
CityLoader (){}
int init(const std::string& filename)
{
SimpleLineReader reader(filename);
reader.ProcessLines(boost::bind(&CityLoader::ProcessLine, this, _1));
printf("readline\t%d\n", city_map_.size());
}
~CityLoader ()
{
}
private:
bool ProcessLine(const std::string& line)
{
static int cnt = ;
if (line.empty())
{
return true;
}
city_map_.insert(make_pair(++cnt, line));
return true;
}
std::map<int, std::string> city_map_;
};
void test_simple_line_reader()
{
CityLoader city_loader;
city_loader.init("data/city.txt");
}
int main()
{
test_simple_line_reader();
}
这就是全部了。个人认为比较方便。
boost::bind 和 boost::function 基本用法的更多相关文章
- boost::bind和boost::function使用示例
C++11已支持bind和function,之前的不支持,但可以借助boost达到同样目的.看如下两段代码: 1) 创建HDFS目录 void hdfs::init() { if (0 == hdfs ...
- 用boost::bind构造boost::coroutine
class TestCoro { ... typedef boost::coroutines::coroutione<void ()> Coro; void CoroFun(Coro::c ...
- [置顶] 编程模仿boost::function和boost::bind
boost::function和boost::bind结合使用是非常强大的,他可以将成员函数和非成员函数绑定对一个对象上,实现了类似C#的委托机制.委托在许多时候可以替代C++里面的继承,实现对象解耦 ...
- 1,Boost -> Bind
#include <boost/bind.hpp> #include <boost/shared_ptr.hpp> #include <iostream> usin ...
- 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的使用方法
bind - boost 头文件: boost/bind.hpp bind 是一组重载的函数模板.用来向一个函数(或函数对象)绑定某些参数. bind的返回值是一个函数对象. 它的源文件太长了. 看不 ...
- boost bind使用指南
bind - boost 头文件: boost/bind.hpp bind 是一组重载的函数模板.用来向一个函数(或函数对象)绑定某些参数. bind的返回值是一个函数对象. 它的源文件太长了. 看不 ...
- boost asio 学习(二)了解boost::bind
2.了解boost::bind使用boost::bind封装一个函数,考虑以下例子示例2a #include <iostream> #include <boost/bind.hpp& ...
- boost::bind四种应用场景的例子
普通函数 int f( int a, int b ){return a + b;}boost::bind( f, _1, 9 )( 1 ) 成员函数 struct demo{int f( in ...
随机推荐
- ubuntu 乱码 改为英文
http://878045653.blog.51cto.com/2693110/735654 解决方法: 改成全英文环境来解决 方格 乱码 : 用vim配置语言环境变量 vim / etc/envir ...
- SH1B LMR62014XMFE/NOPB
制造商National Semiconductor (TI) RoHS 输出电压20 V 输出电流1.4 A 输入电压2.7 V to 14 V 开关频率1.6 MHz 最大工作温度+ 85 C 安装 ...
- IS_ERR、PTR_ERR、ERR_PTR
最近在使用filp_open打开文件时遇到到一个问题,当打开一个并不存在的文件时,filp_open返回值值为0xfffffffe,而并不是0(NULL),这是因为内核对返回指针的函数做了特殊处理.内 ...
- apk反编译(6)ProGuard 工具 android studio版官方教程[作用,配置,解混淆,优化示例]
ProGuard In this document Enabling ProGuard (Gradle Builds) Configuring ProGuard Examples Decoding O ...
- linux/unix网络编程之 poll
转自http://www.cnblogs.com/zhuwbox/p/4222382.html poll 与 select 很类似,都是对描述符进行遍历,查看是否有描述符就绪.如果有就返回就绪文件描述 ...
- Hibernate4.2.2使用Annotation配置
1.在hibernate官网下载hibernate-release-4.2.2.Final.zip并解压 2.新建一个java project工程(20130619_Hibernate4.2.2_An ...
- SetCapture、ReleaseCapture、GetCapture
正常情况下,鼠标指针位于哪个窗口区域内,鼠标消息就自动发给哪个窗口.如果调用了SetCapture,之后无论鼠标的位置在哪,鼠标消息都发给指定的这个窗口,直到调用ReleaseCapture或者调用S ...
- ListView 使用
1. 不使用xml 文件 动态创建 Listview 并且绑定 ArrayList ListView listView = new ListView(this); listView.setAdapte ...
- Hide Xcode8 strange log.
Product > Scheme > Edit Scheme Environment Variables set OS_ACTIVITY_MODE = disable
- andorid 下拉刷新
1:android的下拉刷新操作是需要一个ListView,通过onTouchEvent来判断用户的手势操作,用户触摸屏幕并且下拉时,当下拉超过指定的设定高度时就提示用户进行刷新. 2:当进行刷新时, ...