zoukankan      html  css  js  c++  java
  • STL之函数对象

    摘要:本文主要介绍了函数对象(仿函数)的基本概念,并且举例子对其进行基本的使用。

    1、基本概念

    1.1 什么是函数对象?

    重载函数调用操作符的类,其对象常称为函数对象(function object),即它们是行为类似函数的对象,也叫仿函数(functor),其实就是重载“()”操作符,使得类对象可以像函数那样调用

    1.2 注意

    • 函数对象(仿函数)是一个类,不是一个函数。
    • 函数对象(仿函数)重载了”() ”操作符使得它可以像函数一样调用。

    1.3 分类

    假定某个类有一个重载的operator(),而且重载的operator()要求获取一个参数,我们就将这个类称为“一元仿函数”(unary functor);相反,如果重载的operator()要求获取两个参数,就将这个类称为“二元仿函数”(binary functor)。

    2、STL内建函数对象

    STL内建了一些函数对象。分为:算数类函数对象,关系运算类函数对象,逻辑运算类仿函数。这些仿函数所产生的对象,用法和一般函数完全相同,当然我们还可以产生无名的临时对象来履行函数功能。使用内建函数对象,需要引入头文件 #include<functional>

     

    API 意义
    算术类 template<class T> T plus<T> 加法仿函数
    template<class T> T minus<T> 减法仿函数
    template<class T> T multiplies<T> 乘法仿函数
    template<class T> T divides<T> 除法仿函数
    template<class T> T modulus<T> 取模仿函数
    template<class T> T negate<T> 取反仿函数
    关系运算类  template<class T> bool equal_to<T>  等于
     template<class T> bool not_equal_to<T>  不等于
     template<class T> bool greater<T>  大于
     template<class T> bool greater_equal<T>  大于等于
     template<class T> bool less<T>  小于
     template<class T> bool less_equal<T>  小于等于
    逻辑运算类  template<class T> bool logical_and<T>  逻辑与
    template<class T> bool logical_or<T> 逻辑或
    template<class T> bool logical_not<T> 逻辑非

     

    3、代码示例

    3.1 自定义的函数对象

     1 #include<iostream>
     2 
     3 using namespace std;
     4 
     5 class Myprint {  //定义重载函数调用操作符的类
     6 public:
     7     void operator() (int num){    //重载“()”
     8         cout << "num " << num << endl;
     9         count++;
    10     }
    11     int count = 0;
    12 };
    13 
    14 void test01() {
    15     Myprint myprint;   //定义一个函数对象
    16     myprint(100);   //此时的对象就可以像函数一样调用,注意和有参构造函数相区别
    17     myprint(100);
    18     myprint(100);
    19     cout << myprint.count << endl;  //输出3,表明函数对象可以保存状态
    20     
    21     //还可以使用匿名对象的方式来调用
    22     Myprint()(1000);
    23 }
    24 
    25 //函数对象作为参数
    26 void doprint(Myprint print,int num) {
    27     print(num);
    28 }
    29 
    30 void test02() {
    31     doprint(Myprint(), 10);  //匿名调用方法
    32 
    33     Myprint myprint;
    34     doprint(myprint, 20);    //非匿名调用
    35 }
    36 
    37 int main() {
    38     //test01();
    39     test02();
    40 
    41     system("pause");
    42     return 0;
    43 }

    3.2 内建函数对象

     1 #define _CRT_SECURE_NO_WARNINGS
     2 #include<iostream>
     3 using namespace std;
     4 //内建函数对象头文件
     5 #include <functional>
     6 #include <vector>
     7 #include <algorithm>
     8 
     9 void test01()
    10 {
    11     //template<class T> T negate<T>//取反仿函数
    12     negate<int>n;
    13 
    14     cout << n(10) << endl;
    15 
    16     //加法  template<class T> T plus<T>//加法仿函数
    17 
    18     plus<int> p;
    19 
    20     cout << p(1, 1) << endl;
    21 }
    22 
    23 //template<class T> bool greater<T>//大于
    24 
    25 void test02()
    26 {
    27     vector<int>v;
    28 
    29     v.push_back(10);
    30     v.push_back(30);
    31     v.push_back(50);
    32     v.push_back(20);
    33     v.push_back(40);
    34 
    35     sort(v.begin(), v.end(), greater<int>());
    36 
    37     for_each(v.begin(), v.end(), [](int val){ cout << val << " "; });
    38 }
    39 
    40 int main(){
    41 
    42     //test01();
    43 
    44     test02();
    45 
    46     system("pause");
    47     return EXIT_SUCCESS;
    48 }
  • 相关阅读:
    object-c iOS 教程 git for mac
    mac Git本地服务器配置
    [转]XCode中修改缺省公司名称/开发人员名称
    IOS------Warning
    Linux---CentOS 定时运行脚本配置练手
    微信公众号一些错误的原因错误代码41001
    微信支付的一些新的经验总结
    关于THINKPHP5模型关联的初步理解
    写下thinkphp5和thinkphp3.2的不同
    练手THINKPHP5过程和bootstrap3.3.7
  • 原文地址:https://www.cnblogs.com/lzy820260594/p/11398661.html
Copyright © 2011-2022 走看看