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 ...
随机推荐
- (Android)react-native-splash-screen实践-解决react-native打包好后启动白屏的问题
1.安装 npm i react-native-splash-screen --save or yarn add react-native-splash-screen --save 2.自动配置 re ...
- PHP通过session id 实现session共享和登录验证的代码
先说说,这个机制的用途吧,到现在为止战地知道这个机制有两个方面的用途: 首先,多服务器共享session问题,这个大家应该都能够理解的,当一个网站的用户量过大,就会使用服务器集群,例如专门有一个登录用 ...
- sap 图标查看
showicon这个程序很不错,可以显示SAP里所有的ICON(图标). 用事务码SE38直接运行程序:showicon 即可. 显示列表之后,双击任何一个图标可以显示出每一个图标的详细信息.
- Feature Selection 其一 —— Filter Approach
这一个部分都将只涉及到选择特征的某个子集的方法,将高纬度的特征空间映射到低维度空间的方法(如PCA)都不会涉及到. 一. 单变量 优点:运算速度快,独立于分类器 缺点:忽略的特征之间的联系,忽略了与分 ...
- Java for LeetCode 110 Balanced Binary Tree
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- 20145239 《Java程序设计》实验三 实验报告
详见我的parter20145224的博客:http://www.cnblogs.com/20145224kevs/p/5428892.html 感想:这次的实验看似容易,但很多点都需要注意,比如开源 ...
- POJ2104 K-th Number —— 区间第k小 整体二分
题目链接:https://vjudge.net/problem/POJ-2104 K-th Number Time Limit: 20000MS Memory Limit: 65536K Tota ...
- 9.2 NOIP提高组试题精解(2)
9-18 fruit.c #include <stdio.h> #define MAXN 10000 int Queue1[MAXN], Queue2[MAXN]; void Insert ...
- druid相关的时间序列数据库——也用到了倒排相关的优化技术
Cattell [6] maintains a great summary about existing Scalable SQL and NoSQL data stores. Hu [18] con ...
- 「P4996」「洛谷11月月赛」 咕咕咕(数论
题目描述 小 F 是一个能鸽善鹉的同学,他经常把事情拖到最后一天才去做,导致他的某些日子总是非常匆忙. 比如,时间回溯到了 2018 年 11 月 3 日.小 F 望着自己的任务清单: 看 iG 夺冠 ...