zoukankan      html  css  js  c++  java
  • C++之可调用对象

    C++中的可调用对象分为以下几种:

    • 函数
    • 函数指针
    • lambda表达式
    • bind创建的对象
    • 重载了函数调用运算符(即“()”)的类
    函数、函数指针不再介绍。lambda表达式与bind创建的类参考——lambda表达式和bind函数。
     
    这里通过例子说明一下lambda表达式与重载了函数调用运算符的类有一些相通之处。
    lambda是函数对象,是一个可调用对象。编译器在解释lambda表达式时,将其解释为一个未命名类的未命名对象,该类重载了函数调用运算符。
    1、首先声明一个类,重载函数调用运算符。成员变量根据实际情况而定。
    1. /**
    2. * 重载了函数调用运算符的类
    3. */
    4. classSizeComp
    5. {
    6. public:
    7. SizeComp(std::size_t sz): size(sz){}
    8. voidoperator()(const string & str)const
    9. {
    10. if(str.size()>= size)
    11. {
    12. cout << str << endl;
    13. }
    14. }
    15. private:
    16. std::size_t size;
    17. };
    2、创建类对象,并给需要用到的变量进行赋值。
    1. void testInvoke2nd()
    2. {
    3. vector<string> strVec ={"1","12","123","1234","12345","123456","1234567"};
    4. std::size_t sz =5;
    5. cout <<"lambda表达式:"<< endl;
    6. auto comp =[sz](const string & str)->void
    7. {
    8. if(str.size()>= sz)
    9. {
    10. cout << str << endl;
    11. }
    12. };
    13. for_each(strVec.begin(), strVec.end(), comp);
    14. cout <<"()运算符重载:"<< endl;
    15. for_each(strVec.begin(), strVec.end(),SizeComp(sz)); // 先是调用构造函数创建一个临时对象
    16. }
     
    对可调用对象的使用
    1. #include<iostream>
    2. #include<functional>
    3. #include<map>
    4. usingnamespace std;
    5. /**< 可调用对象——函数 */
    6. /**
    7. * 若进行函数重载则不能直接使用函数名
    8. * 会产生二义性
    9. * 此时考虑函数指针或其他的实现方式
    10. */
    11. //implements "+" operation
    12. constint addInt(constint x,constint y)
    13. {
    14. return x + y;
    15. }
    16. /**< 可调用对象——重载了调用运算符的类 */
    17. //implements "-" operation
    18. struct minusInt
    19. {
    20. constintoperator()(constint x,constint y)
    21. {
    22. return x - y;
    23. }
    24. };
    25. //函数指针所指向的函数
    26. constint dividesFunc(constint x,constint y)
    27. {
    28. return x / y;
    29. }
    30. void testFunction()
    31. {
    32. /**< 可调用对象——lambda表达式 */
    33. //implements "*" operation
    34. auto multipInt =[](constint x,constint y)->constint
    35. {
    36. return x * y;
    37. };
    38. /**< 可调用对象——函数指针 */
    39. //implements "/" operation
    40. constint(*dividesInt)(int,int)= dividesFunc;
    41. // dividesInt = multipInt; // 说明lambda是一个函数对象
    42. /**
    43. * function<int(int, int)>的原型是function<T>
    44. * @brief 存储可调用对象(该对象的调用形式应该与T相同)
    45. * @param T 函数类型(返回值类型,参数个数及)
    46. */
    47. map<string, function<int(int,int)>> binops =
    48. {
    49. {"+", addInt},/* 函数名默认转化为函数指针类型 */
    50. {"-", minusInt()},/* 创建一个类对象 */
    51. {"*", multipInt},/* lambda表达式类型的对象 */
    52. {"/", dividesInt}/* 函数指针 */
    53. };
    54. cout << binops["+"](10,5)<< endl
    55. << binops["-"](10,5)<< endl
    56. << binops["*"](10,5)<< endl
    57. << binops["/"](10,5)<< endl;
    58. }
    59. int main()
    60. {
    61. testFunction();
    62. return0;
    63. }
     
     





  • 相关阅读:
    修改服务器时间及时区
    风情万种awk
    追忆似水流年sed
    lvm笔记
    正则不怎么会用
    在CentOS7.5的虚拟环境下新建你的django项目
    Django学习过程中的排错总结
    我一直记不住的vim用法
    关于自建yum源拾遗
    Djangp2.x版本报错找不到模版目录下的文件
  • 原文地址:https://www.cnblogs.com/fengkang1008/p/4648344.html
Copyright © 2011-2022 走看看