利用STL的set容器进行升序排序和降序排序--源码

11 篇文章 0 订阅

利用STL的set容器进行升序排序和降序排序--源码--VS2019下运行测试成功:

#include <iostream>
#include <set>
using namespace std;

struct myfunc 
{
    bool operator()(int v1,int v2) const
    {
        return v1 > v2;
    }
};

int main()
{
    //set升序排列
    set<int> Tmp;
    //初始化
    Tmp.insert(8);
    Tmp.insert(10);
    Tmp.insert(100);
    Tmp.insert(9);
    Tmp.insert(20);
    Tmp.insert(3);

    for (set<int>::iterator it = Tmp.begin(); it != Tmp.end(); ++it)
    {
        cout << "升序排列:" << *it << endl;
    }


    //set降序排列
    set<int, myfunc> setTmp;
    //初始化
    setTmp.insert(8);
    setTmp.insert(10);
    setTmp.insert(100);
    setTmp.insert(9);
    setTmp.insert(20);
    setTmp.insert(3);

    for (set<int,myfunc>::iterator it = setTmp.begin();  it != setTmp.end(); ++it)
    {
        cout << "降序排列:" << *it << endl;
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值