为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>的更多相关文章

  1. 浅谈JSP中include指令与include动作标识的区别

    JSP中主要包含三大指令,分别是page,include,taglib.本篇主要提及include指令. include指令使用格式:<%@ include file="文件的绝对路径 ...

  2. Entity Framework 6 Recipes 2nd Edition(13-9)译 -> 避免Include

    问题 你想不用Include()方法,立即加载一下相关的集合,并想通过EF的CodeFirst方式实现. 解决方案 假设你有一个如Figure 13-14所示的模型: Figure 13-14. A ...

  3. error RC1015: cannot open include file 'afxres.h' 解决办法

    在为WindowsPhone8程序添加本地化的过程中遇到这个问题: 问题原因就是afxres.h文件缺失,下载它,放到VS安装目录下的VS\include目录下就可以了(选择目录的时候注意对应对版本) ...

  4. Mybatis常用总结:参数,返回,执行sql,include等

    1.参数注入1.1用#{0},#{1}的形式,0代表第一个参数,1代表第二个参数 public List<RecordVo> queryList(String workerId, Inte ...

  5. jsp中的@include与jsp:include区别详解

    1 前言 搞java开发的人也许都知道在jsp中引入项目中其他文件有如下两种方式 <%@include file="xxx.jsp"%> <jsp:include ...

  6. JSP中编译指令include与动作指令include的区别

    include指令是编译阶段的指令,即include所包含的文件的内容是编译的时候插入到JSP文件中,JSP引擎在判断JSP页面未被修改, 否则视为已被修改.由于被包含的文件是在编译时才插入的,因此如 ...

  7. C/C++ 中的include

    当需要使用已有的方法或库时, 可以将它们的头文件#include进来. #include会在preprocess过程中被替换成它包含的代码. 头文件中包含了需要使用的函数/变量的声明. 当然声明与定义 ...

  8. 织梦多语言站点,{dede:include filename=''/}引入问题

    织梦模板include插入非模板目录文件出现"无法在这个位置找到"错误的解决办法 以下是dede V55_UTF8 查dede include标签手册 (3) include 引入 ...

  9. PHP 站点相对包含,路径的问题解决方法(include,require)

    以前看了,很多框架,基本上很少使用相对路径包含.而一般很多做php web站点,喜欢用相对路径. 认为这样,无论目录放到那里. 只要跟另外目录关系一致.那么就不会出现问题.如果一个站点,一般都认为,如 ...

  10. 如何让include标签包裹的布局置于屏幕最下方?

    如何让一个Layout 始终在屏幕的下方 我想让<include layout="@layout/bottom" />一直在屏幕下,怎么做? 1.相对布局中用属性  a ...

随机推荐

  1. Reverse Linked List 解答

    Question Reverse a singly linked list. Solution 1 -- Iterative Remember to set head.next = null or i ...

  2. C++对象模型浅析

    本文仅代表博主自己对C++内存对象模型的一点理解,如果文中有 理解偏差和不准确的地方,希望各位大大提出,我好及时改正. 本博文只对博主自己负责,不对任何人负责. 就如<深度探索C++对象模型&g ...

  3. oracle获取某一字段字符串长度

    用length方法 select t.* from tp_area t where substr(t.area_id,0,2)='03' and length(t.area_id)>2

  4. HBase 6、用Phoenix Java api操作HBase

    开发环境准备:eclipse3.5.jdk1.7.window8.hadoop2.2.0.hbase0.98.0.2.phoenix4.3.0 1.从集群拷贝以下文件:core-site.xml.hb ...

  5. C#向文件写、读数据

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  6. Linux 文件操作——系统调用和标准I/O库

    一.什么是文件 在讲述文件操作之前,我们首先要知道什么是文件.看到这个问题你可能会感觉到可笑,因为对于用过计算机的人来说,文件是最简单不过的概念了,例如一个文本是一个文件,一个work文档是一个文件等 ...

  7. 从Android Handler内部类到WeakReference的知识关联

    Handler: 普通使用方法: Handler用于处理和从队列MessageQueue中得到Message.一般我们要重写Handler的handleMessage(Message msg){}方法 ...

  8. Maven 工程下 Spring MVC 站点配置 (一)

    最近,查找一些具体资料时,虽然会有很多,但是系统的却很少,尤其是对maven 下 spring mvc 站点搭建的配置,总是说的很多但让新手一目了然的步骤却少之又少. 对此闲暇时整理了一下,做了一套较 ...

  9. ionic安装

    npm set registry http://registry.cnpmjs.org/ 设置淘宝镜像

  10. 在SQL中修改数据库名称

    假设SQL Server 2008中有个数据库test,现在要将其改名为zhy步骤:(1) 分离数据库:打开management studio,找到test数据库-->右键-->任务--& ...