STL自定义排序小结

前言:

我们要先明白在哪个地方需要使用自定义排序:

  • sort()函数的第三个参数需要自定义函数
  • 有序set,map容器需要自定义排序

一:sort()函数

sort对vector容器排序时,第三个参数为一个函数,函数内容即为排序的要求。

1:当对可直接比较变量排序时,默认为升序:

#include <bits/stdc++.h>
using namespace std;
//排序函数,自定义为降序
bool cmp2 (const int a, const int b) {
    return a > b;
}

int main() {
    freopen("i.txt","r",stdin);
    vector<int> vector2;
    vector2.push_back(1); 
    vector2.push_back(2);
    vector2.push_back(3);
    sort(vector2.begin(), vector2.end(), cmp2);
    for(int i = 0; i < vector2.size(); i++)
        cout << vector2[i] << endl;

}

2:对对象进行排序时,可以在类中使用重载运算符,也可以使用排序函数。

#include <bits/stdc++.h>
using namespace std;
struct Node {
public:
    int a,b;
    ~Node() {}
    Node(int _a, int _b):a(_a), b(_b) {}
	//重载运算符
    bool operator < (const Node& node) const {
        return a > node.a;
    }

};

//排序函数,注意,默认的排序方式为内部的重载运算符定义的排序方式
//sort使用cmp会替换默认的排序方式
bool cmp (const Node node1, const Node node2) {
    return node1.a < node2.a;
}

int main() {
    freopen("i.txt","r",stdin);
    vector<Node> vector1;
    vector1.emplace_back(1,5);
    vector1.emplace_back(2,3);
    vector1.emplace_back(3,4);
    sort(vector1.begin(), vector1.end(),cmp);
    for(int i = 0; i < vector1.size(); i++)
        cout << vector1[i].a << " " << vector1[i].b << endl;

}

二:关联容器

1:set存储可直接比较变量。

#include <bits/stdc++.h>
using namespace std;
//根据字符串长度排序
struct cmp {
    bool operator () (const string& a, const string& b) const {
        return a.length() > b.length();
    }
};

int main() {
    freopen("i.txt","r",stdin);
    set<string, cmp> set1;
    set1.insert("aaa");
    set1.insert("b");
    for(auto it=set1.begin(); it!=set1.end(); it++)
        cout << *it << endl;
}

2:set存储对象

#include <bits/stdc++.h>
using namespace std;

struct Node {
public:
    string str;
    int a;
    ~Node() {}
    Node(string _str, int _a):
        str(_str), a(_a) {}

    bool operator < (const Node& a) const {
        return a.str.length() > str.length();
    }

};

struct cmp {
    bool operator () (const Node& a, const Node& b) const {
        return a.str.length() > b.str.length();
    }
};

int main() {
    freopen("i.txt","r",stdin);
    set<Node, cmp> set1;
    set1.insert(Node("aaa",5));
    set1.insert(Node("b",5));
    for(auto it=set1.begin(); it!=set1.end(); it++)
        cout << it->str << endl;
}

3:map和set一样,但是map默认为对值key进行排序,这里我们按照value进行排序。

#include<bits/stdc++.h>
using namespace std;


bool cmp(const pair<string, int>& a, const pair<string, int>& b) {
    return a.second < b.second;
}

int main()
{
    //1、map这里指定less作为其默认比较函数(对象),就是默认按键值升序排列
    map<string, int> map1;
    map1.insert({"aaa",1});
    map1.insert({"bbb",-1});

    //输出添加的内容
    for (auto iter = map1.begin(); iter != map1.end(); ++iter) {
        cout << (*iter).first << endl;
    }
    cout << endl;
    // 将map中的内容转存到vector中
    vector<pair<string, int>> vec(map1.begin(), map1.end());
    //对线性的vector进行排序
    sort(vec.begin(), vec.end(), cmp);
    for (int i = 0; i < vec.size(); ++i)
        cout << vec[i].first << " " << vec[i].second << endl;
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值