std::function"函数"对象包装器
语义:
类模板std::function是可调用对象的包装器,可以包装除了类成员之外的所有可调用对象。包括,普通函数,函数指针,lambda,仿函数。通过指定的模板参数,它可以用统一的方式保存,并延迟执行它们。所谓的延迟执行,就是回调了。
它使得C++对C的兼容性更强了。
常规多态案例:
#include <iostream>
#include <functional>
using namespace std;
class Operator
{
public:
virtual int op(int,int) = ;
};
class OperatorAdd:public Operator
{
public:
int op(int i,int j)
{
return i+j;
}
};
class OperatorMinus:public Operator
{
public:
int op(int i,int j)
{
return i-j;
}
};
int main()
{
Operator *oper = new OperatorAdd;
cout<<oper->op(,)<<endl;
oper = new OperatorMinus;
cout<<oper->op(,)<<endl;
return ;
}
多态转std::function
#include <iostream>
#include <functional>
#include <map>
using namespace std;
int add(int i,int j)
{
return i+j;
}
int _minus(int i,int j)
{
return i-j;
} typedef int(*MINUS)(int,int);
//using MINUS = int(*)(int,int)
auto multiply = [](int i,int j){return i*j;}; class Divide
{
public:
int operator()(int i, int j){
return i/j;
}
};
int main()
{
std::function<int(int,int)> oper;
oper = add;
cout<<oper(,)<<endl;
MINUS m = _minus;
oper = m;
cout<<oper(,)<<endl;
oper = multiply;
cout<<oper(,)<<endl;
oper = Divide();
cout<<oper(,)<<endl;
map<string,std::function<int(int,int)>> math;
math.insert({"+",add});
math.insert({"-",_minus});
math.insert({"*",multiply});
math.insert({"/",Divide()});
math.insert({"%",[](int i, int j){return i%j;}});
cout<<math["+"](,)<<endl;
cout<<math["-"](,)<<endl;
cout<<math["*"](,)<<endl;
cout<<math["/"](,)<<endl;
cout<<math["%"](,)<<endl;
return ;
}
写完这段代码完全被震撼了,被感动的不要不要的。C++的灵活性简直逆天了。
应用:
常规回调
#include <iostream>
#include <functional>
using namespace std;
class functor
{
public:
void operator()()
{
cout<<__FUNCTION__<<endl;
}
}; class A
{
public:
A(const function<void()> & cb):_callback(cb)
{}
void notify()
{
_callback();
}
function<void()> _callback;
};
int main(int argc, char *argv[])
{
functor fct;
A a(fct);
a.notify();
return ;
}
#include <iostream>
#include <functional> using namespace std; void printWhenEven(int data,const std::function<void(int)> &f)
{
if(data%)
f(data);
} void print(int i)
{
cout<<i<<endl;
} int main()
{
for(int i =;i<;i++)
{
printWhenEven(i,print);
cout<<"+++++++++++++++++++"<<endl;
// printWhenEven(i,[](int i){cout<<i+1<<endl;});
}
}
std::function"函数"对象包装器的更多相关文章
- C++11多态函数对象包装器
[C++11多态函数对象包装器] 针对函数对象的多态包装器(又称多态函数对象包装器)在语义和语法上和函数指针相似,但不像函数指针那么狭隘.只要能被调用,且其参数能与包装器兼容的都能以多态函数对象包装器 ...
- c++11——std::function和bind绑定器
c++11中增加了std::function和std::bind,可更加方便的使用标准库,同时也可方便的进行延时求值. 可调用对象 c++中的可调用对象存在以下几类: (1)函数指针 (2)具有ope ...
- C++11 学习笔记 std::function和bind绑定器
C++11 学习笔记 std::function和bind绑定器 一.std::function C++中的可调用对象虽然具有比较统一操作形式(除了类成员指针之外,都是后面加括号进行调用),但定义方法 ...
- Java基础(八)对象包装器与自动装箱
1.对象包装器 有时候,需要将int这样的基本类型转换为对象.所有的基本类型都有一个与之对应的类.通常,这些类被称为包装器(wrapper). 这些对象包装类分别是:Integer.Long.Floa ...
- 第12课 std::bind和std::function(3)_std::function可调用对象包装器
1. std::function (1)首先是一个类模板,用于包装可调用对象.可以容纳除了类成员(函数)指针之外的所有可调用对象. (2)可以将普通函数,lambda表达式和函数对象类统一起来.尽管它 ...
- 关于Function()函数对象的那些小九九
概念:首先,函数是一种特殊类型的数据,函数也是数据类型的一种,实际上函数也是一种对象,函数对象的内建构造器是Function(); 函数的几种创建方式: 函数声明法: function sum(a,b ...
- python3 functools partial 用于函数的包装器详解
一.partial 的作用: partial 用于对一个已有函数进行包装,达到功能的定制的目的. 二.例子: 假设我们要完成两个功能,第一个功能是完成两个数相加,第二个功能是给一个自增一下 1.传统方 ...
- 剖析std::function接口与实现
目录 前言 一.std::function的原理与接口 1.1 std::function是函数包装器 1.2 C++注重运行时效率 1.3 用函数指针实现多态 1.4 std::function的接 ...
- C++11新特性之八——函数对象function
详细请看<C++ Primer plus>(第六版中文版) http://www.cnblogs.com/lvpengms/archive/2011/02/21/1960078.html ...
随机推荐
- Complete space 完备空间与柯西序列 巴拿赫空间与完备空间 完备空间与和希尔伯特空间 封闭closed与完备性complete
http://www.gatsby.ucl.ac.uk/~gretton/coursefiles/RKHS2013_slides1.pdf RKHS: a function space with a ...
- Chernoff-Hoeffding inequality -- Chernoff bounds, and some applications
https://www.cs.utah.edu/~jeffp/teaching/cs5955/L3-Chern-Hoeff.pdf [大数据-通过随机过程降维 ] When dealing with ...
- AppStore App申请审核加速
容芳志大牛一直是我学习的榜样 分类: iOS开发经验技巧2014-11-12 09:40 409人阅读 评论(0) 收藏 举报 有没有遇到上线后发现很严重的bug这种情况,修复bug后提交审核又是漫长 ...
- 多线程(三) iOS中的锁
锁的类别:互斥锁,递归锁,条件锁,自旋锁等 锁的实现方式:NSLock,NSRecursiveLock, NSConditionLock,@synchronized,GCD的信号量等 下面说一下常用的 ...
- POJ 之 WERTYU
WERTYU Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8371 Accepted: 4007 Descriptio ...
- 粉红色织梦CMS企业模板
粉红色织梦CMS企业网站模板,粉红色,织梦CMS,织梦企业模板,CMS模板. 模板地址:http://www.huiyi8.com/sc/7247.html
- 破解 Navicat Premium 12
一.下载 若文件百度云链接失效,请发邮件给博主:1766211120@qq.com 1.安装文件下载 v12.0.11(x64)版本下载地址如下 链接:https://pan.baidu.com/s/ ...
- Apache-POI 简单应用
测试的Excel文件为四列的普通表格 jar包:poi-3.15-beta2.jar(Office2003xls文件).poi-ooxml-3.15-beta2.jar(Office2007xlsx文 ...
- spring的了解以及简单框架的搭建
了解spring: Spring是一个开源的控制反转(Inversion of Controller)和面向切面(AOP)的框架,目的是为了简化开发. IOC(控制反转): public class ...
- codeforces 569B B. Inventory(水题)
题目链接: B. Inventory time limit per test 1 second memory limit per test 256 megabytes input standard i ...