zoukankan      html  css  js  c++  java
  • STL内建函数对象使用 functional

    内建函数对象

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

    #include<functional>

    1.

    #define _CRT_SECURE_NO_WARNINGS
    #include <iostream>
    using namespace std;
    #include <functional>
    #include <vector>
    #include <algorithm>
    /*
    6个算数类函数对象,除了negate是一元运算,其他都是二元运算。
    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>//取反仿函数
    
    6个关系运算类函数对象,每一种都是二元运算。
    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>//小于等于
    
    逻辑运算类运算函数,not为一元运算,其余为二元运算。
    template<class T> bool logical_and<T>//逻辑与
    template<class T> bool logical_or<T>//逻辑或
    template<class T> bool logical_not<T>//逻辑非
    
    */
    void test01()
    {
        //template<class T> T negate<T>//取反仿函数  一元
        negate<int> n;
        cout << n(10) << endl;      //-10
    
        //加法 template<class T> T plus<T>//加法仿函数  二元
        plus<int> p;
        cout << p(2, 3) << endl;    //5
    }
    void test02()
    {
        vector<int>v;
        v.push_back(20);
        v.push_back(40);
        v.push_back(10);
        v.push_back(30);
        v.push_back(50);
    
        sort(v.begin(), v.end(), greater<int>());       //使用内建函数 降序排序
        for_each(v.begin(), v.end(), [](int v) {cout << v << " "; });       //使用lambda打印
    }
    int main()
    {
        test02();
        //test01();
        system("Pause");
        return 0;
    }

    结果:

  • 相关阅读:
    BZOJ 1724: [Usaco2006 Nov]Fence Repair 切割木板 贪心 + 堆 + 反向思考
    BZOJ 1715: [Usaco2006 Dec]Wormholes 虫洞 DFS版SPFA判负环
    qqq
    爬虫的盗亦有道Robots协议
    Requests库
    常用的re模块的正则匹配的表达式
    python -服务器与客户端断电续传程序详细介绍
    模拟ssh远程执行命令,粘包问题,基于socketserver实现并发的socket
    python大佬养成计划----基于flask_sqlalchemy的网页显示数据库信息
    python实战----Todo清单续写
  • 原文地址:https://www.cnblogs.com/yifengs/p/15194691.html
Copyright © 2011-2022 走看看