zoukankan      html  css  js  c++  java
  • list-sort1

    ////////////////////////////////////////
    //      2018/04/27 7:26:06
    //      list-sort1
    
    // sorts the list
    #include <iostream>
    #include <list>
    #include <algorithm>
    #include <functional>
    
    using namespace std;
    
    template <class T>
    class Print{
    public:
        void operator()(T& t){
            cout << t << " ";
        }
    };
    //=====================
    int main(){
        int ary[] = { 3, 2, 5, 7, 3, 6, 7, 2, 4, 5 };
        list<int> li(ary, ary + 10);
        Print<int> print;
    
        cout << "Before sorting
     li:";
        for_each(li.begin(),li.end(), print);
        cout << endl;
    
        li.sort(greater<int>());
        cout << "After li.sort(greater<int>())
    li:";
        for_each(li.begin(), li.end(), print);
        cout << endl;
    
        li.sort(less<int>());
        cout << "After li.sort(less<int>())
    li:";
        for_each(li.begin(), li.end(), print);
        cout << endl;
    
        return 0;
    }
    /*
    OUTPUT:
        Before sorting
        li:3 2 5 7 3 6 7 2 4 5
        After li.sort(greater<int>())
        li:7 7 6 5 5 4 3 3 2 2
        After li.sort(less<int>())
        li:2 2 3 3 4 5 5 6 7 7
    */ 
    
    /*
    
    // TEMPLATE STRUCT greater
        emplate<class _Ty>
        struct greater
            : public binary_function<_Ty, _Ty, bool>
        {   // functor for operator>
            bool operator()(const _Ty& _Left, const _Ty& _Right) const
            {   // apply operator> to operands
                return (_Left > _Right);
            }
        };
    
        // TEMPLATE STRUCT less
        emplate<class _Ty>
            struct less
                : public binary_function<_Ty, _Ty, bool>
        {   // functor for operator<
            bool operator()(const _Ty& _Left, const _Ty& _Right) const
            {   // apply operator< to operands
                return (_Left < _Right);
            }
        };
    */
  • 相关阅读:
    Java 基础
    Java 数据类型
    Spring 拦截器实现事物
    SSH 配置日记
    Hibernate 知识提高
    Jsp、Servlet
    leetcode 97. Interleaving String
    leetcode 750. Number Of Corner Rectangles
    leetcode 748. Shortest Completing Word
    leetcode 746. Min Cost Climbing Stairs
  • 原文地址:https://www.cnblogs.com/laohaozi/p/12537933.html
Copyright © 2011-2022 走看看