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 ...
随机推荐
- php整理(三): 面向对象
PHP学习(三)----面向对象 首先,还是建立一个好的理解模型: 1.什么是面向对象? 面向对象分为两个部分,那就是:什么是对象和什么是面向? 什么是对象: 对象的出现就是为了用代码更好的绘制我 ...
- Android 中Activity生命周期分析:Android中横竖屏切换时的生命周期过程
最近在面试Android,今天出了一个这样的题目,即如题: 我当时以为生命周期是这样的: onCreate --> onStart -- ---> onResume ---> onP ...
- sql server 数据库 ' ' 附近有语法错误
昨天做项目时候,遇到标题的问题,代码跟踪把sql 语句 复制出来在数据库执行不了, 然后重新写个一模一样的,然后在 赋值到代码中,还是同样的错误, 就是不知道哪里出现了错误,最后 把 sql 语句写成 ...
- EXT 数据按F12,F11 显示问题
最近做关于EXT的项目,因为是刚开始接触EXT,对什么都不熟悉,所以把其他人写好的浏览页代码考过了来,换成自己需要的. 一切都做好了,然后数据不出来,就调试看,后台也出现数据了,然后就按F12调试前台 ...
- css动画集合地址
CSS3 UI Lib库是由腾讯AlloyTeam前端开发团队建立,主要收集国内外友好体验和创意的界面组件Demo. 它除了使用CSS3技术外,还使用了HTML5,JS,JX,jQuery等技术,来达 ...
- [转] 搜索之双向BFS
转自:http://www.cppblog.com/Yuan/archive/2011/02/23/140553.aspx 如果目标也已知的话,用双向BFS能很大程度上提高速度. 单向时,是 b^le ...
- linux shell ls -1 列显示文件
/******************************************************************************* * linux shell ls -1 ...
- I.MX6 lcd lvds hdmi bootargs
/********************************************************************* * I.MX6 lcd lvds hdmi bootarg ...
- 【C#学习笔记】播放wma/mp3文件
using System; using System.Runtime.InteropServices; namespace ConsoleApplication { class Program { [ ...
- ffmepg 指定RTSP网络连接模式UDP还是TCP
AVFormatContext *formatCtx = NULL; formatCtx = avformat_alloc_context(); AVDictionary* options = NUL ...