转自:https://blog.csdn.net/shuilan0066/article/details/82788954

  1. 示例1 普通函数
  1. void gFunc()
  1. {
  1. cout << "gFunc" << endl;
  1. }
  1. int main()
  1. {
  1. std::function<void()> f = gFunc;
  1. f();
  1. getchar();
  1. return 0;
  1. }
  1.  
  1. 示例2 模板函数
  2.  
  3. template <class T>
  4. T g_Add(T i, T j)
  5. {
  6.   cout << i + j;
  7.   return i + j;
  8. }
  9.  
  10. int main()
  11. {
  12.   std::function<int(int,int)> f = g_Add<int>;
  13.   f(,);
  14.  
  15.   getchar();
  16.   return ;
  17. }
  1. 示例三: 匿名函数
  2.  
  3. auto g_Lambda = [](int i, int j)
  4. {
  5.   return i + j;
  6. }; //匿名函数 此处有分号
  7.  
  8. int main()
  9. {
  10.   std::function<int(int, int)> f = g_Lambda;
  11.   cout<<f(,);
  12.  
  13.   getchar();
  14.   return ;
  15. }
  1. 示例四:函数对象
  2.  
  3. /函数对象
  4. struct Add
  5. {
  6.   int operator()(int i, int j)
  7.   {
  8.     return i + j;
  9.   }
  10. };
  11.  
  12. //模板函数对象
  13. template <class T>
  14. struct AddT
  15. {
  16.   T operator()(T i, T j)
  17.   {
  18.     return i + j;
  19.   }
  20. };
  21.  
  22. int main()
  23. {
  24.   std::function<int(int, int)> f = Add();
  25.   cout<<f(,)<<endl;
  26.  
  27.   std::function<int(int, int)> ft = AddT<int>();
  28.   cout << ft(, )<<endl;
  29.  
  30.   getchar();
  31.   return ;
  32. }
  1. 示例5:类成员函数
  2.  
  3. class Computer
  4. {
  5.   public:
  6.   static int Add(int i, int j)
  7.   {
  8.     return i + j;
  9.   }
  10.  
  11.   template<class T>
  12.   static T AddT(T i, T j)
  13.   {
  14.     return i + j;
  15.   }
  16.  
  17.   int AddN(int i, int j)
  18.   {
  19.     return i + j;
  20.   }
  21. };
  22.  
  23. //存储对成员函数的调用
  24.  
  25. int main()
  26. {
  27.   //1、 类静态函数
  28.   std::function<int(int, int)> f = &Computer::Add;
  29.   cout << f(, ) << endl;
  30.  
  31.   //2、 类静态模板函数
  32.   std::function<int(int, int)> ft = &Computer::AddT<int>;
  33.   cout << ft(, ) << endl;
  34.  
  35.   //普通函数绑定 需要构造类对象
  36.   Computer c;
  37.  
  38.   //3、 普通函数 需使用bind,将类对象地址 &c 绑定上
  39.   std::function<int(int, int)> fN = std::bind(&Computer::AddN, &c, placeholders::_1, placeholders::_2);
  40.   cout << fN(, ) << endl;
  41.  
  42.   //4、普通函数, 也可以这样调用 个人觉得这个比 bind 麻烦,不建议
  43.   std::function <int(const Computer &, int, int)> fN2 = &Computer::AddN;
  44.   cout << fN2(c,, ) << endl;
  45.  
  46.   getchar();
  47.   return ;
  48. }

std::function以及std::bind的更多相关文章

  1. std::function,std::bind

    std::function 和 std::bind 标准库函数bind()和function()定义于头文件中(该头文件还包括许多其他函数对象),用于处理函数及函数参数.bind()接受一个函数(或者 ...

  2. C++ 中std::function 、std::bind的使用和lambda的使用

    std::function是可调用对象的包装器:std::bind是将可点用对象和其参数一起进行绑定,且绑定后的结果可以使用std::function对象进行保存,并延迟调用到需要调用的时候: 在C+ ...

  3. C++11 std::function、std::bind和lambda表达式

    参考博客: C++可调用对象详解-https://www.cnblogs.com/Philip-Tell-Truth/p/5814213.html 一.关于std::function与std::bin ...

  4. C++11新特性应用--实现延时求值(std::function和std::bind)

    说是延时求值,注意还是想搞一搞std::function和std::bind. 之前博客<C++11新特性之std::function>注意是std::function怎样实现回调函数. ...

  5. c++11 符号修饰与函数签名、函数指针、匿名函数、仿函数、std::function与std::bind

    一.符号修饰与函数签名 1.符号修饰 编译器将c++源代码编译成目标文件时,用函数签名的信息对函数名进行改编,形成修饰名.GCC的C++符号修饰方法如下: 1)所有符号都以_z开头 2)名字空间的名字 ...

  6. C++11之std::function和std::bind

    std::function是可调用对象的包装器,它最重要的功能是实现延时调用: #include "stdafx.h" #include<iostream>// std ...

  7. std::function与std::bind 函数指针

    function模板类和bind模板函数,使用它们可以实现类似函数指针的功能,但却却比函数指针更加灵活,特别是函数指向类 的非静态成员函数时. std::function可以绑定到全局函数/类静态成员 ...

  8. 转 C++11之std::function和std::bind

    std::function是可调用对象的包装器,它最重要的功能是实现延时调用: #include "stdafx.h" #include<iostream>// std ...

  9. 【浅析C++11】std::function和std::bind

    目录 std::function可调用对象包装器 std::function基本用法 std::function/std::bind与抽象工厂.工厂方法的一点思考 std::function可调用对象 ...

随机推荐

  1. java基础(3)---Scanner键盘输入

    1.使用scanner类: import java.util.Scanner; class ScannerTest{ public static void main( String[] args){ ...

  2. 胡搞-强化版的light oj-1055-的思路-AI版的6重暴力For循环的BFS

    新题目大意: 三个棋子按照先后顺序,可以随意方向合法地走到空位置上(而不是像原题light oj-1055中的一样三个棋子每次走的方向都一致),当三个棋子全部走进目标地点,就结束:求需要指挥的最少次数 ...

  3. Linux添加shell(.sh)脚本并添加定时任务

    一.添加sheel脚本 1.首先创建一个执行程序:vim a.sh 2.编辑: #!/bin/bash  python3  python.py >> test2.log 2>& ...

  4. python开发的百度翻译接口

    做的一个python版的百度翻译,附代码 #!/usr/bin/env python # -*- coding:utf-8 -*-   ''' 爬虫之百度翻译 需要的库有 js2py, request ...

  5. datafram 操作集锦

    Spark Python API 官方文档中文版> 之 pyspark.sql (二) 2017-11-04 22:13 by 牛仔裤的夏天, 365 阅读, 0 评论, 收藏, 编辑 摘要:在 ...

  6. sql server 子查询 和exists使用

    概述 子查询的概念: 当一个查询是另一个查询的条件时,称之为子查询.子查询可以嵌套在主查询中所有位置,包括SELECT.FROM.WHERE.GROUP BY.HAVING.ORDER BY. 外面的 ...

  7. 接口实现后台GZIP压缩,pako.js 前端解压

    import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.IOException; ...

  8. Elasticsearch 调优之 搜索速度优化

    本章讨论搜索速度优化:搜索速度与系统资源.数据索引方式.查询方式等多方面 1.为文件系统cache预留足够的内存 1)应用程序一般情况下,读写都会被操作系统“cache” 2)cache保存在物理内存 ...

  9. 搭建自己的博客(九):使用shell模式批量添加博客文章并增加分页功能

    想做个博客分页功能,但是没有太多的文章.所以使用shell命令行创建多篇文章. 1.打开pycharm下的terminal终端 python manage.py shell # 打开python终端 ...

  10. 数据结构实验之查找五:平方之哈希表 (SDUT 3377)

    Hash表的平方探测思路:如果当前这个没存放数值,就放进去,如果当前这个地方Hash [ i ] 已经有数值了,就以平方的间隔左右寻找没有存放数的空白 Hash [ i ]. #include < ...