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);
            }
        };
    */
  • 相关阅读:
    第八周进度条
    对老师的评价
    构建之法阅读笔记03
    构建之法阅读笔记02
    第七周进度条
    团队冲刺第二周07
    团队冲刺第二周06
    Java jdbc 连接oracle
    Java 生成验证码
    Oracle 触发器的简单命令
  • 原文地址:https://www.cnblogs.com/laohaozi/p/12537934.html
Copyright © 2011-2022 走看看