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\n li:";
    for_each(li.begin(),li.end(), print);
    cout << endl;

    li.sort(greater<int>());
    cout << "After li.sort(greater<int>())\nli:";
    for_each(li.begin(), li.end(), print);
    cout << endl;

    li.sort(less<int>());
    cout << "After li.sort(less<int>())\nli:";
    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);
        }
    };
*/
posted @ 2018-04-27 07:46  老耗子  阅读(44)  评论(0)    收藏  举报