#include <boost/function.hpp>
为atoi取别名fun,fun实质上是函数指针
#include <iostream>
#include <boost/function.hpp> void main()
{
boost::function<int(char *)>fun = atoi;//为atoi取别名fun,fun实质上是函数指针 std::cout << fun("") + fun("") << std::endl; fun = strlen; std::cout << fun("") + fun("") << std::endl;
}
结合boost::bind使用
#include <iostream>
#include <boost/bind.hpp>
#include <boost/function.hpp> void main()
{
boost::function<int(char *)>fun; fun = boost::bind(strcmp, "ABC", _1);//绑定strcmp,判断和"ABC"是否相同 std::cout << fun("") << std::endl; std::cout << fun("ABC") << std::endl;
}
function和bind配合使用可以很方便的实现类成员回调,极好的应用于一些需要回调的场合。
#include <iostream>
#include <boost/bind.hpp>
#include <boost/function.hpp> class manager
{
public:
void allstart()
{
for (int i = ; i < ; i++)
{
if (workid)
{
workid(i);
}
}
}
void setcallback(boost::function<void(int)>newid)//绑定调用
{
workid = newid;
}
public:
boost::function<void(int)> workid;
}; class worker
{
public:
void run(int toid)
{
id = toid;
std::cout << id << "工作" << std::endl;
}
public:
int id;
}; void main()
{
manager m;
worker w; m.setcallback(boost::bind(&worker::run, &w, _1));//绑定
//类的成员函数需要对象来调用,绑定了一个默认的对象 m.allstart();
}
ref库
当在某些情况下需要拷贝对象参数时,如果该对象无法进行拷贝,或者拷贝代价过高,这时候就可以选择ref
#include <iostream>
#include <vector>
#include <algorithm>
#include <boost/bind.hpp>
#include <boost/function.hpp> void print(std::ostream &os, int i)
{
os << i << std::endl;
} void main()
{
boost::function<void(int)>pt = boost::bind(print, boost::ref(std::cout), _1);//std::cout不能拷贝,所以用ref std::vector<int>v;
v.push_back();
v.push_back();
v.push_back(); for_each(v.begin(), v.end(), pt);
}
#include <boost/function.hpp>的更多相关文章
- 浅谈JSP中include指令与include动作标识的区别
JSP中主要包含三大指令,分别是page,include,taglib.本篇主要提及include指令. include指令使用格式:<%@ include file="文件的绝对路径 ...
- Entity Framework 6 Recipes 2nd Edition(13-9)译 -> 避免Include
问题 你想不用Include()方法,立即加载一下相关的集合,并想通过EF的CodeFirst方式实现. 解决方案 假设你有一个如Figure 13-14所示的模型: Figure 13-14. A ...
- error RC1015: cannot open include file 'afxres.h' 解决办法
在为WindowsPhone8程序添加本地化的过程中遇到这个问题: 问题原因就是afxres.h文件缺失,下载它,放到VS安装目录下的VS\include目录下就可以了(选择目录的时候注意对应对版本) ...
- Mybatis常用总结:参数,返回,执行sql,include等
1.参数注入1.1用#{0},#{1}的形式,0代表第一个参数,1代表第二个参数 public List<RecordVo> queryList(String workerId, Inte ...
- jsp中的@include与jsp:include区别详解
1 前言 搞java开发的人也许都知道在jsp中引入项目中其他文件有如下两种方式 <%@include file="xxx.jsp"%> <jsp:include ...
- JSP中编译指令include与动作指令include的区别
include指令是编译阶段的指令,即include所包含的文件的内容是编译的时候插入到JSP文件中,JSP引擎在判断JSP页面未被修改, 否则视为已被修改.由于被包含的文件是在编译时才插入的,因此如 ...
- C/C++ 中的include
当需要使用已有的方法或库时, 可以将它们的头文件#include进来. #include会在preprocess过程中被替换成它包含的代码. 头文件中包含了需要使用的函数/变量的声明. 当然声明与定义 ...
- 织梦多语言站点,{dede:include filename=''/}引入问题
织梦模板include插入非模板目录文件出现"无法在这个位置找到"错误的解决办法 以下是dede V55_UTF8 查dede include标签手册 (3) include 引入 ...
- PHP 站点相对包含,路径的问题解决方法(include,require)
以前看了,很多框架,基本上很少使用相对路径包含.而一般很多做php web站点,喜欢用相对路径. 认为这样,无论目录放到那里. 只要跟另外目录关系一致.那么就不会出现问题.如果一个站点,一般都认为,如 ...
- 如何让include标签包裹的布局置于屏幕最下方?
如何让一个Layout 始终在屏幕的下方 我想让<include layout="@layout/bottom" />一直在屏幕下,怎么做? 1.相对布局中用属性 a ...
随机推荐
- 百度地图LV1.5实践项目开发工具类bmap.util.jsV1.3
/** * 百度地图使用工具类-v1.5 * * @author boonya * @date 2013-7-7 * @address Chengdu,Sichuan,China * @email b ...
- LintCode-A + B 用位操作模拟加法
class Solution { public: /* * @param a: The first integer * @param b: The second integer * @return: ...
- 【转】linux环境变量设置
1. 显示环境变量HOME $ echo $HOME /home/terry 2. 设置一个新的环境变量WELCOME $ export WELCOME="Hello!" $ ec ...
- 微软GitHub组织
微软aspnet团队的GitHub 微软dotnet团队的GitHub 微软的GitHub 微软云团队的GitHub 微软在GitHub的开源底部有其它组织
- UILabel Text 加下划线
.h文件 #import <Foundation/Foundation.h> @interface CustomLabel : UILabel { BOOL _isEnabled; } @ ...
- [转]Traceroute网络排障实用指南(1)
注:本文是同事的大作,虽是翻译的一篇英文PPT,但内容实在精彩,小小的Traceroute竟包含如此大的信息量,真是让人感慨!内容不涉及公司机密,所以一直想转到自己的Blog上来,自己需要时可以再翻阅 ...
- Android中ExpandableListView控件基本使用
本文採用一个Demo来展示Android中ExpandableListView控件的使用,如怎样在组/子ListView中绑定数据源.直接上代码例如以下: 程序结构图: layout文件夹下的 mai ...
- u3d shader使用
先建立一个材质球Material 选择shader 把材质球Material 赋给图片
- rhel5.8安装mysql测试
MySQL-rhel5.8 安装:在Linux下安装MySQL有三种方式:第一种以rpm的二进制文件分个安装,第二种是自己编译源码后安装,最后一种是以二进制tar.gz文件来安装(这种安装方式下载安装 ...
- git 用户手册
Git是一个分布式版本控制/软件配置管理软件,原来是linux内核开发者林纳斯·托瓦兹为了更好地管理linux内核开发而创立的.需要注意的是和GNU Interactive Tools,一个类似Nor ...