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 ...
随机推荐
- svn服务的安装和使用
更新linux软件库 cat /etc/redhat-release wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.c ...
- 【题解】CF264B Good Sequences
[题解]CF264B Good Sequences 具有很明显的无后效性. 考虑\(dp\). 考虑初始条件,显然是\(dp(0)=0\) 考虑转移,显然是\(dp(t)=max(dp[k])+1\) ...
- [STM8L]基于STM8L152的TAB段式LCD液晶驱动的分析 - 单片机干货 - 中国电子技术论坛 - 最好最受欢迎电子论坛!
[STM8L]基于STM8L152的TAB段式LCD液晶驱动的分析 - 单片机干货 - 中国电子技术论坛 - 最好最受欢迎电子论坛!.md 主控芯片为STM8L152C4T6自带LCD控制器,低功耗系 ...
- 《avascript 高级程序设计(第三版)》 ---第三章 基本概念2
1.乘性操作符: 1)*法操作法: Infinity * 0 = NaN Infinity * 非零 = Infinity 或 - Infinity 2)/法操作符: Infinity / In ...
- mybatis中xml字段空判断及模糊查询
由于业务特殊的查询需求,需要下面的这种查询,一直感觉模糊不清,本地测试一下顺便做个总结 贴一段xml代码,如下: <if test="receivedName != null and ...
- Adding Form Fields to a MS Word Document
Configuring a Word Merge in SmartSimple is a three-step process: Create the MS Word document that wi ...
- PYTHON 爬虫笔记九:利用Ajax+正则表达式+BeautifulSoup爬取今日头条街拍图集(实战项目二)
利用Ajax+正则表达式+BeautifulSoup爬取今日头条街拍图集 目标站点分析 今日头条这类的网站制作,从数据形式,CSS样式都是通过数据接口的样式来决定的,所以它的抓取方法和其他网页的抓取方 ...
- iOS开发数据库-FMDB
前言 FMDB是以OC的方式封装了SQLite的C语言API,使用起来更加面向对象,省去了很多麻烦.冗余的C语言代码:对比苹果自带的Core Data框架,更加轻量级和灵活:提供了多线程安全的数据库操 ...
- 时尚创意VI矢量设计模板
时尚创意VI矢量设计模板 创意VI VI设计 企业VI 时尚背景 信封设计 封面设计 杯子 桌旗 帽子 EPS矢量素材下载 http://www.huiyi8.com/vi/
- DropDownList(For)
1.绑定数据源 方法一 Controllers:var users = GetUsers(); var selectList = new SelectList(users, "Value&q ...