C++11 function使用
function是一组函数对象包装类的模板,实现了一个泛型的回调机制。
引入头文件
#include <functional>
using namespace std;
using namespace std::placeholders; //bind的时候会用`
参考:http://www.cnblogs.com/hujian/archive/2012/12/07/2807605.html
fuction bind:http://blog.csdn.net/fjb2080/article/details/7527715
我们可以调用的对象有很多,比如普通函数、函数指针、lanmbda表达式、函数对象和类的成员函数等。
不管采用哪种方式,主要调用形式一样(返回值类型、传递给调用的实参类型),我们就可以使用同一种形式来调用。
这个时候就可以用到function模板,它给予我们在调用的方式上更大的弹性。
请看一下三种不同的函数定义:
- int add(int a, int b){
- return a+b;
- }
- auto mod=[](int a, int b){return a%b;};
- struct divide{
- int operator()(int m, int n){
- return m/n;
- }
- };
这三种都可以使用同一种调用形式,int(int, int),调用方式如下:
- function<int(int,int)> func1= add;
- function<int(int,int)> func2= divide();
- function<int(int,int)> func3= mod;
- cout<<func1(5, 6)<<endl;
- cout<<func2(5, 6)<<endl;
- cout<<func3(5, 6)<<endl;
学会了使用function,可以继续如下进行抽象定义,不同类型采用相同的调用方法:
- map<string,function<int(int, int)>> funs =
- {
- {"+", add},
- {"-", std::minus<int>()},//标准库的函数,参数为两个整数,可以参考前一篇博客
- {"/", divide()},//类成员函数
- {"*", [](int i,int j){return i*j;}},//lambda表达式
- {"%", mod},
- };
- funs["+"](4,6);
以上就是function的简单使用。下面是从另一篇博客转载的,使用function的引用来保存函数对象。考虑下面代码:
- class CAdd
- {
- public:
- CAdd():m_nSum(0){NULL;}
- int operator()(int i)
- {
- m_nSum += i;
- return m_nSum;
- }
- int Sum() const
- {
- return m_nSum;
- }
- private:
- int m_nSum;
- };
- int main(int argc, const char * argv[])
- {
- CAdd cAdd;
- function<int(int)> funcAdd1 = cAdd;
- function<int(int)> funcAdd2 = cAdd;
- cout<<funcAdd1(10)<<endl;
- cout<<funcAdd2(10)<<endl;
- cout<<cAdd.Sum()<<endl;
- return 0;
- }
上面的输出结果是 10 10 0。我们将同一个函数对象赋值给了两个function,然后分别调用这两个function,但函数中的成员变量的值没有保存,问题在哪里?因为function的缺省行为是拷贝一份传递给它的函数对象,于是f1,f2中保存的都是cAdd对象的拷贝。
C++11提供了ref和cref函数来提供对象的引用和常引用的包装。要是function能够正确保存函数对象的状态,可以如下修改代码:
- function<int(int)> funcAdd3 = ref(cAdd);
- function<int(int)> funcAdd4 = ref(cAdd);
- cout<<funcAdd3(10)<<endl;
- cout<<funcAdd4(10)<<endl;
- cout<<cAdd.Sum()<<endl;
另外,两个function之间赋值时,如果源function保存的是函数对象的拷贝,则目标function保存的也是函数对象的拷贝。如果源function保存的是对函数对象的引用,则目标function保存的也是函数对象的引用。
C++11 function使用的更多相关文章
- 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, ...
- C++11 function
#include <iostream> #include <functional> #include <vector> using namespace std; / ...
- c++11 function bind 测试。
实验小结 1)function 是一个模板类.有函数指针成员.可以看作安全型函数指针. template<typename _Res, typename... _ArgTypes> cla ...
- c++11:function的用法
function是函数.函数对象.函数指针.和成员函数的包装器,可以容纳任何类型的函数对象,函数指针,引用函数,成员函数的指针 普通函数 #include <functional> voi ...
- C++ 11: function & bind 使用示例
#include <functional> #include <iostream> struct Foo { Foo(int num) : num_(num) {} void ...
- C++11 function用法 可调用对象模板类
std::function<datatype()> ()内写参数类型 datatype 代表function的返回值 灵活的用法.. 代码如下 #include <stdio.h&g ...
- C++11绑定器bind及function机制
前言 之前在学muduo网络库时,看到陈硕以基于对象编程的方式,大量使用boost库中的bind和function机制,如今,这些概念都已引入至C++11,包含在头文件<functional&g ...
- JavaScript function函数种类(转)
转自:http://www.cnblogs.com/polk6/p/3284839.html JavaScript function函数种类 本篇主要介绍普通函数.匿名函数.闭包函数 目录 1. 普通 ...
随机推荐
- go new() 和 make() 的区别
看起来二者没有什么区别,都在堆上分配内存,但是它们的行为不同,适用于不同的类型. new(T) 为每个新的类型T分配一片内存,初始化为 0 并且返回类型为*T的内存地址:这种方法 返回一个指向类型为 ...
- Windows下运行jekyll,编码已不再是问题
很久没更新jekyll了,所以好奇着去官网看了下更新记录,发现如下更新条目(版本1.3.0/2013-11-04发布): Add encoding configuration option (#144 ...
- vue 使用vue-i18n做全局中英文切换
1.vue-i18n安装 npm install vue-i18n --save-dev 2.在main.js文件中引入 import VueI18n from 'vue-i18n'; Vue.use ...
- git 的 基础操作及使用
/* git svn版本控制器 */ /*git把文件对应的储存空间分为三个区: 1.工作区 2.缓存区 3.历史区 直接操作文件,不做add时,咱们是在工作区做的修改 右键 git bash her ...
- 洛谷——P1168 中位数
P1168 中位数 题目描述 给出一个长度为NN的非负整数序列$A_i$,对于所有1 ≤ k ≤ (N + 1),输出$A_1, A_3, …, A_{2k - 1}A1,A3,…,A2k−1 ...
- LINUX-挂载一个文件系统
mount /dev/hda2 /mnt/hda2 挂载一个叫做hda2的盘 - 确定目录 '/ mnt/hda2' 已经存在 umount /dev/hda2 卸载一个叫做hda2的盘 - 先从挂载 ...
- OS X中微信双开
1.打开系统终端: 2.下载插件,输入命令(不包括括号) git clone https://github.com/Sunnyyoung/WeChatTweak-macOS.git 并回车: 3.进入 ...
- saturne installation on Ubuntu
test installation Table of Contents 1. installation guide 1 installation guide saturne_installation. ...
- PAT 1141 PAT Ranking of Institutions
After each PAT, the PAT Center will announce the ranking of institutions based on their students' pe ...
- json转换时区问题-------前端展示时间少8小时--解决方法
在application配置文件中加如下: spring.jackson.time-zone=GMT+8