为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. 64位Win7安装+32位Oracle + PL/SQL 解决方法

    软件景象:64位win7.32位Oracle 10g. PL/SQL 9.0.4.1644 媒介:以前开辟用的都是32位体系,忽然换到64位上,安装景象真的有点麻烦了,尤其对于PL/SQL只支撑32位 ...

  2. C语言随笔_printf输出多行

    想在printf中,输出多行数据,如果写成下面这样: printf("line 1\n line 2\n line 3\n");编译器会报错“error C2001: newlin ...

  3. First Missing Positive 解答

    Question Given an unsorted integer array, find the first missing positive integer. For example,Given ...

  4. Word Ladder II 解答

    Question Given two words (beginWord and endWord), and a dictionary's word list, find all shortest tr ...

  5. (转)ZOJ 3687 The Review Plan I(禁为排列)

    The Review Plan I Time Limit: 5 Seconds      Memory Limit: 65536 KB Michael takes the Discrete Mathe ...

  6. UGUI Image控件

    今天一起学习Image控件O(∩_∩)O~ 介绍一下基本的属性 Source:Image:               指定图片源, 图片设置2DSprite(2D and UI)格式Color:   ...

  7. Unity 移动MM自签名方式

    在使用Unity接移动MM SDK的时候,最后有一个签名.  主要是把计费文件和版权文件放入APK的根目录.  搞了半天才知道前来这么简单..... 软件使用: apk签名工具apktool

  8. lua select

    可以用这样的方法产生类似foreach的功能: function printargs(...) local num_args = select("#", ...) for i = ...

  9. Starting httpd:Could not reliably determine the server's fully qualified domain name

    #service httpd start #Starting httpd: httpd: Could not reliably determine the server's fully qualifi ...

  10. Node.js 入门教程和学习资源汇总

    这篇文章与大家分享一批很有用的 Node.js 入门教程和学习资源.Node 是一个服务器端的 JavaScript 解释器,它将改变服务器应该如何工作的概念.它的目标是帮助程序员构建高度可伸缩的应用 ...