std::function以及std::bind
转自:https://blog.csdn.net/shuilan0066/article/details/82788954
- 示例1 : 普通函数
- void gFunc()
- {
- cout << "gFunc" << endl;
- }
- int main()
- {
- std::function<void()> f = gFunc;
- f();
- getchar();
- return 0;
- }
- 示例2 模板函数
- template <class T>
- T g_Add(T i, T j)
- {
- cout << i + j;
- return i + j;
- }
- int main()
- {
- std::function<int(int,int)> f = g_Add<int>;
- f(,);
- getchar();
- return ;
- }
- 示例三: 匿名函数
- auto g_Lambda = [](int i, int j)
- {
- return i + j;
- }; //匿名函数 此处有分号
- int main()
- {
- std::function<int(int, int)> f = g_Lambda;
- cout<<f(,);
- getchar();
- return ;
- }
- 示例四:函数对象
- /函数对象
- struct Add
- {
- int operator()(int i, int j)
- {
- return i + j;
- }
- };
- //模板函数对象
- template <class T>
- struct AddT
- {
- T operator()(T i, T j)
- {
- return i + j;
- }
- };
- int main()
- {
- std::function<int(int, int)> f = Add();
- cout<<f(,)<<endl;
- std::function<int(int, int)> ft = AddT<int>();
- cout << ft(, )<<endl;
- getchar();
- return ;
- }
- 示例5:类成员函数
- class Computer
- {
- public:
- static int Add(int i, int j)
- {
- return i + j;
- }
- template<class T>
- static T AddT(T i, T j)
- {
- return i + j;
- }
- int AddN(int i, int j)
- {
- return i + j;
- }
- };
- //存储对成员函数的调用
- int main()
- {
- //1、 类静态函数
- std::function<int(int, int)> f = &Computer::Add;
- cout << f(, ) << endl;
- //2、 类静态模板函数
- std::function<int(int, int)> ft = &Computer::AddT<int>;
- cout << ft(, ) << endl;
- //普通函数绑定 需要构造类对象
- Computer c;
- //3、 普通函数 需使用bind,将类对象地址 &c 绑定上
- std::function<int(int, int)> fN = std::bind(&Computer::AddN, &c, placeholders::_1, placeholders::_2);
- cout << fN(, ) << endl;
- //4、普通函数, 也可以这样调用 个人觉得这个比 bind 麻烦,不建议
- std::function <int(const Computer &, int, int)> fN2 = &Computer::AddN;
- cout << fN2(c,, ) << endl;
- getchar();
- return ;
- }
std::function以及std::bind的更多相关文章
- std::function,std::bind
std::function 和 std::bind 标准库函数bind()和function()定义于头文件中(该头文件还包括许多其他函数对象),用于处理函数及函数参数.bind()接受一个函数(或者 ...
- C++ 中std::function 、std::bind的使用和lambda的使用
std::function是可调用对象的包装器:std::bind是将可点用对象和其参数一起进行绑定,且绑定后的结果可以使用std::function对象进行保存,并延迟调用到需要调用的时候: 在C+ ...
- C++11 std::function、std::bind和lambda表达式
参考博客: C++可调用对象详解-https://www.cnblogs.com/Philip-Tell-Truth/p/5814213.html 一.关于std::function与std::bin ...
- C++11新特性应用--实现延时求值(std::function和std::bind)
说是延时求值,注意还是想搞一搞std::function和std::bind. 之前博客<C++11新特性之std::function>注意是std::function怎样实现回调函数. ...
- c++11 符号修饰与函数签名、函数指针、匿名函数、仿函数、std::function与std::bind
一.符号修饰与函数签名 1.符号修饰 编译器将c++源代码编译成目标文件时,用函数签名的信息对函数名进行改编,形成修饰名.GCC的C++符号修饰方法如下: 1)所有符号都以_z开头 2)名字空间的名字 ...
- C++11之std::function和std::bind
std::function是可调用对象的包装器,它最重要的功能是实现延时调用: #include "stdafx.h" #include<iostream>// std ...
- std::function与std::bind 函数指针
function模板类和bind模板函数,使用它们可以实现类似函数指针的功能,但却却比函数指针更加灵活,特别是函数指向类 的非静态成员函数时. std::function可以绑定到全局函数/类静态成员 ...
- 转 C++11之std::function和std::bind
std::function是可调用对象的包装器,它最重要的功能是实现延时调用: #include "stdafx.h" #include<iostream>// std ...
- 【浅析C++11】std::function和std::bind
目录 std::function可调用对象包装器 std::function基本用法 std::function/std::bind与抽象工厂.工厂方法的一点思考 std::function可调用对象 ...
随机推荐
- java基础(3)---Scanner键盘输入
1.使用scanner类: import java.util.Scanner; class ScannerTest{ public static void main( String[] args){ ...
- 胡搞-强化版的light oj-1055-的思路-AI版的6重暴力For循环的BFS
新题目大意: 三个棋子按照先后顺序,可以随意方向合法地走到空位置上(而不是像原题light oj-1055中的一样三个棋子每次走的方向都一致),当三个棋子全部走进目标地点,就结束:求需要指挥的最少次数 ...
- Linux添加shell(.sh)脚本并添加定时任务
一.添加sheel脚本 1.首先创建一个执行程序:vim a.sh 2.编辑: #!/bin/bash python3 python.py >> test2.log 2>& ...
- python开发的百度翻译接口
做的一个python版的百度翻译,附代码 #!/usr/bin/env python # -*- coding:utf-8 -*- ''' 爬虫之百度翻译 需要的库有 js2py, request ...
- datafram 操作集锦
Spark Python API 官方文档中文版> 之 pyspark.sql (二) 2017-11-04 22:13 by 牛仔裤的夏天, 365 阅读, 0 评论, 收藏, 编辑 摘要:在 ...
- sql server 子查询 和exists使用
概述 子查询的概念: 当一个查询是另一个查询的条件时,称之为子查询.子查询可以嵌套在主查询中所有位置,包括SELECT.FROM.WHERE.GROUP BY.HAVING.ORDER BY. 外面的 ...
- 接口实现后台GZIP压缩,pako.js 前端解压
import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.IOException; ...
- Elasticsearch 调优之 搜索速度优化
本章讨论搜索速度优化:搜索速度与系统资源.数据索引方式.查询方式等多方面 1.为文件系统cache预留足够的内存 1)应用程序一般情况下,读写都会被操作系统“cache” 2)cache保存在物理内存 ...
- 搭建自己的博客(九):使用shell模式批量添加博客文章并增加分页功能
想做个博客分页功能,但是没有太多的文章.所以使用shell命令行创建多篇文章. 1.打开pycharm下的terminal终端 python manage.py shell # 打开python终端 ...
- 数据结构实验之查找五:平方之哈希表 (SDUT 3377)
Hash表的平方探测思路:如果当前这个没存放数值,就放进去,如果当前这个地方Hash [ i ] 已经有数值了,就以平方的间隔左右寻找没有存放数的空白 Hash [ i ]. #include < ...