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;
}

 

转载于:https://www.cnblogs.com/zhanggaofeng/p/10993298.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值