zoukankan      html  css  js  c++  java
  • C++ STL排序问题

    /* stl排序 */
    #include <iostream>
    #include <map>
    #include <vector>
    #include <list>
    #include <algorithm>
    
    using namespace std;
    
    struct STComp :public binary_function<int, int, bool>
    {
        inline bool operator()(int x, int y)
        {
            //返回true,表示不用变更位置,从大到小排序
            return x > y;
        }
    
    };
    
    struct STPrint :public unary_function<int, bool>
    {
        inline void operator()(int x)
        {
            cout << x << " " ;
        }
    };
    
    void test1()
    {
        vector<int> v1 = { 1,2,3,4,5,6,7,8 };
        //vector排序
        sort(v1.begin(), v1.end(), STComp());
        for_each(v1.begin(), v1.end(), STPrint());
    }
    
    void test2()
    {
        std::list<int> alist;
    
        alist.push_back(3);
        alist.push_back(4);
        alist.push_back(2);
        alist.push_back(7);
        alist.push_back(9);
        alist.push_back(1);
        //list排序
        alist.sort(STComp());
        for_each(alist.begin(), alist.end(), STPrint());
    }
    
    int main()
    {
        test2();
        getchar();
        return 0;
    }
  • 相关阅读:
    7、python数据类型之集合set
    python基本数据类型练习
    matplotlib
    numpy常用函数
    pillow包
    keras-tensorflow版本对应
    python-激活和切换运行环境
    端口监控
    numpy
    低风险创业笔记
  • 原文地址:https://www.cnblogs.com/zhanggaofeng/p/10993298.html
Copyright © 2011-2022 走看看