c++11:function的用法
function是函数、函数对象、函数指针、和成员函数的包装器,可以容纳任何类型的函数对象,函数指针,引用函数,成员函数的指针
普通函数
#include <functional>
void print_num(int i)
{
cout << "i" << endl;
} function<void(int)> f_display = print_num;
f_display(-); function<void()> f_display_42 = [](){ print_num(); };
f_display_42(); function<void()> f_display_32337 = bind(print_num, ); //对bind不明白参考bind
f_display_32337();
类成员函数
#include <functional>
stuct Foo {
Foo(int num) : num_(num) {}
void print_add(int i) const { cout << num_ + i << endl;}
int num_;
}; function<void(const Foo&, int)> f_add_display = &Foo::print_add;
const Foo foo();
f_add_display(foo, ); function<void(int)> f_add_display2 = bind(&Foo::print_add, foo, placeholders::_1);
f_add_display2();
一个实际的应用:
#include <functional>
#include <iostream> using namespace std; class Scope {
public:
explicit Scope(function<void()> o) :
on_exit_(o) {}
~Scope() { on_exit_(); }
private:
function<void()> on_exit_;
}; int main()
{
Scope scope([]() { cout << "close" << endl; });
}
输出结果: close
c++11:function的用法的更多相关文章
- boost::function的用法
本片文章主要介绍boost::function的用法. boost::function 就是一个函数的包装器(function wrapper),用来定义函数对象. 1. 介绍 Boost.Func ...
- boost::bind 和 boost::function 基本用法
这是一篇介绍bind和function用法的文章,起因是近来读陈硕的文章,提到用bind和function替代继承,于是就熟悉了下bind和function的用法,都是一些网上都有的知识,记录一下,期 ...
- c/c++ 重载运算符 标准库function的用法
重载运算符 标准库function的用法 问题:int(int, int)算不算一种比较通用的类型?? 比如函数: int add(int a, int b); 比如lambda:auto mod = ...
- python3新特性函数注释Function Annotations用法分析
本文分析了python3新特性函数注释Function Annotations用法.分享给大家供大家参考,具体如下: Python 3.X新增加了一个特性(Feature),叫作函数注释 Functi ...
- C++11 function用法 可调用对象模板类
std::function<datatype()> ()内写参数类型 datatype 代表function的返回值 灵活的用法.. 代码如下 #include <stdio.h&g ...
- C++11新特性之二——std::bind std::function 高级用法
/* * File: main.cpp * Author: Vicky.H * Email: eclipser@163.com */ #include <iostream> #includ ...
- ECharts中color : function的用法(转)
ECharts图表实战经验1:如何设置图表同序列不同数据点的独立颜色值 最近有不少朋友在追问这样一个问题:我单序列的柱状图,我想让每一个根柱子的颜色都不一样,应该如何做? 针对这个问题,其实我只想 ...
- C++11 Function 使用场景
[1]场景分析 在一个函数内部,可能会多次用到某一段代码,一般情况是把这段用到次数较多的代码封装成一个函数. 但是,如果这段代码仅仅只在这个函数中有使用,这时封装成函数显得既麻烦又冗赘. 那么,有没有 ...
- Using C++11 function & bind
The example of callback in C++11 is shown below. #include <functional> void MyFunc1(int val1, ...
随机推荐
- QtInternal 之 高效使用QString
注意:本文翻译自 http://developer.qt.nokia.com 中的 UsingQStringEffectively ,中文译文见 简体中文版 ,如果你对翻译wiki感兴趣 ...
- windows下搭建svn服务端、客户端
1.安装SVN服务器subversion以及客户端TortoiseSVN,在网上下载windows版的subversion,TortoiseSVN并安装,比如我的服务端安装在了D:\Program F ...
- B. Pasha and String
time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...
- git 删除已经 add 的文件
使用 git rm 命令即可,有两种选择, 一种是 git rm --cached "文件路径",不删除物理文件,仅将该文件从缓存中删除: 一种是 git rm --f " ...
- Java Script基础(四) BOM模型
一.BOM模型 BOM模型(Browser Object Model),也称为文档对象模型,它包含浏览器相关的属性和方法,例如操作,前进后退按钮,控制地址栏,关闭浏览器窗口,打开新窗口等等.它包含的对 ...
- 【MongoDB】增删改查基本操作
查看所有数据库 show dbs 切换数据库(若不存在,会自动创建) use databasename 删除当前数据库 db.dropDatabase() MongoDB中没有表,只有集合. 插入集合 ...
- PHP提示Deprecated: mysql_connect(): The mysql extension is deprecated的解决方法
这篇文章主要介绍了PHP提示Deprecated: mysql_connect(): The mysql extension is deprecated的解决方法,是在进行PHP数据库程序开发中常会遇 ...
- 怒刷DP之 HDU 1087
Super Jumping! Jumping! Jumping! Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64 ...
- js实现shell排序
//shell排序配插入排序function shell_insert_sort(arr){ var gap = arr.length; do{ gap = parseInt(gap/3) + 1; ...
- Oracle数据库作业-2 添加主键 外键
一.在表student中添加主键sno