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, ...
随机推荐
- Web安全XSS
Web安全XSS 简单的反射型XSS钓鱼演示 </form> <script> function hack(){ XSSImage=new Image; XSSImage.sr ...
- gamework的使用方法
翻译来源地址:https://github.com/Kadoba/gamework gamework是控制LOVE2D游戏进程流的一个项目. ↑ 这个是按原文译的, 当初乍看完全不懂, 接下来我来用图 ...
- 标准库 - fmt/format.go 解读
// Copyright 2009 The Go Authors. All rights reserved. // Use of this source code is governed by a B ...
- F - Tree
Description You are to determine the value of the leaf node in a given binary tree that is the termi ...
- How to setup ELM327 Bluetooth WiFi for Android software Torque
1. Install OBDII 2. Install Android Software Torque a) Copy software to phone from CD b) ...
- 【阿里云产品公测】云引擎ACE新手实战基于Wordpress
[阿里云产品公测]云引擎ACE新手实战基于Wordpress 作者:阿里云用户imnpc ACE(Aliyun Cloud Engine) 是一款弹性.分布式的应用托管环境,支持Java.php多种语 ...
- Unix系统安装MySQL-python出现UnicodeDecodeError错误解决方法
今天装MySQL-python时候出现了这个错误: error: command ---------------------------------------- Cleaning up... Com ...
- 重构15-Remove Duplication(删除重复)
这大概是处理一个方法在多处使用时最常见的重构.如果不加以注意的话,你会慢慢地养成重复的习惯.开发者常常由于懒惰或者在想要尽快生成尽可能多的代码时,向代码中添加很多重复的内容.我想也没必要过多解释了吧, ...
- codeforces 579D D. "Or" Game(前后缀+贪心)
题目链接: D. "Or" Game time limit per test 2 seconds memory limit per test 256 megabytes input ...
- hibernate的第一个程序
#建表语句 create database hibernate; use hibernate; create table user( id int primary key, name varchar( ...