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 ...
随机推荐
- python类似微信未读信息图片脚本
其实就是实现一个效果,给一张图片,然后再右上角给出未读的信息数目,就像我们打开微信的时候,总是看到红点就忍不住想要点击去查看一样. 类似这种效果: 可以知道,图片是给定的,那么只要随机生成一个数字,然 ...
- tcpdump抓SQL
前言:假设如果有个服务器几十个链接突然达到上千个链接,show processlist,general_log,还有慢查询日志这些都不能用,你怎么把这些链接过来的SQL情况了解清楚,如果你觉得那些好用 ...
- html5 touch事件实现触屏页面上下滑动(一)
最近做的做那个app的项目由于用overflow:hidden导致了很多问题,于是决定研究下html5的touch事件.想找个全面点的帖子真是难死了,虽然好多关于html5 touch的文章但大多都是 ...
- BZOJ 3140 消毒(最小顶点覆盖)
题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=3140 题意:最近在生物实验室工作的小T遇到了大麻烦. 由于实验室最近升级的缘故,他的分格 ...
- Oracle数据泵导入导出数据,建立表空
Oracle11g 数据导入到oracle10g 中:1.在oracle11g 服务器命令行中用expdp 导出数据expdp ts/ts@orcl directory=expdp_dir dumpf ...
- sdut 2831 Euclid (几何)
题目:http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2831 题意:给a, b, c, d, ...
- Android开发性能优化大总结
1. 采用硬件加速,在androidmanifest.xml中application添加android:hardwareAccelerated="true".不过这个需要在and ...
- JAVA将Excel中的报表导出为图片格式(三)换一种实现
上一篇介绍了使用Java的Robot机器人实现截图,然后将剪贴板上的数据流生成PNG图片 但是经过博主的不断测试,在完全依赖远程桌面的没有终端显示器的服务器上 使用截图方式是不可行的,因为一旦使用了远 ...
- jquery dialog-优雅的弹出框
前面一章已经对datepicker的使用,做了简单的说明.这一章主要对dialog如何使用做个说明. jquery ui-dialog在web开发中运用还是比较多的.最常见的例子就是登 ...
- Java Web编程的主要组件技术——JDBC
参考书籍:<J2EE开源编程精要15讲> JDBC(Java DataBase Connectivity)是Java Web应用程序开发的最主要API之一.当向数据库查询数据时,Java应 ...