目录
前言
在写代码时候遇到container需要排序的问题,发现sort在STL中应用很有意思,这里讨论在vector 和 priority_queue中的应用,主要通过代码实现的角度来尝试讨论下面的内容。
- vector:
对于int类型数据,从default(<), less<int>(), greater<int>() 三个方面使用sort;
对于自定义类型数据,从重载sort使用数据类型中的operator<, 自定义compare function, 自定义compare class()三个方面使用sort。
- priority_queue:
定义一个priority_queue时,默认是大根堆,可以通过priority_queue<T, vector<T>,classname>第三个参数改写内部的sort,实现自定义的小根堆或者大根堆。
对于int类型数据,从default(== less<int>), less<int>, greater<int> 三个方面;
对于自定义类型数据,从重载数据类型中的operator<, 自定义compare function //实践证明错误用法, 自定义compare class三个方面。
代码
/**
* @file operator.cpp
* @author Stephen Ding (1103037805@qq.com)
* @brief Method to Implement TrieTree With CPP
* @version 0.1
* @date 2022-04-10
*
* @copyright Copyright (c) 2022
*
*/
#include <iostream>
#include <queue>
#include <functional> // std:: less && greater<int>
#include <vector>
#include <algorithm> // std::sort
using namespace std;
struct Agent{
Agent(int a_i, int b_i){
a = a_i;
b = b_i;
}
int a;
int b;
// 1.重载运算符<
bool operator< (const Agent& rhs) const {
return this->a < rhs.a;
}
};
//2.比较函数<
bool CompareFunc(const Agent& lhs, const Agent& rhs){
return lhs.a < rhs.a;
}
//3.重写仿函数
struct CompareClass{
// default public:
bool operator()(const Agent& lhs, const Agent& rhs){
return lhs.a > rhs.a; // > 类似greater<int>
// return lhs.a < rhs.a; // < less<int>
}
};
// 打印Agent
template <typename T>
void PrintAgent(const T& arg){
bool valid = false;
for(auto elem : arg){
cout << elem.a << "\t";
valid = true;
}
if(!valid){
cout << "no data !";
}
cout << endl;
}
// 打印Int
template <typename T>
void PrintInt(const T& arg){
bool valid = false;
for(auto elem : arg){
cout << elem << "\t";
valid = true;
}
if(!valid){
cout << "no data !";
}
cout << endl;
}
int main(int argc, char** argv){
// 使用vector
string s(60, '#');
cout << s << endl;
vector<int> a = {10, 1, 10000, 1000};
a.at(1) = 100;
cout << "vector<int> not sort: " << endl;
PrintInt(a);
vector<int> b(a);
cout << "vector<int> default: " << endl;
sort(b.begin(),b.end()); // 默认
PrintInt(b);
vector<int> c(a);
cout << "vector<int> less<int>: " << endl;
sort(c.begin(),c.end(),less<int>()); // less
PrintInt(c);
vector<int> d(a);
cout << "vector<int> greater<int>: " << endl;
sort(d.begin(),d.end(),greater<int>()); // greater
PrintInt(d);
vector<Agent> t;
Agent t_i(10,2);
t.emplace_back(t_i);
t_i.a = 10000;
t.emplace_back(t_i);
t_i.a = 100;
t.emplace_back(t_i);
t_i.a = 1000;
t.emplace_back(t_i);
cout << "vector<Agent> not sort: " << endl;
PrintAgent(t);
vector<Agent> t1(t);
cout << "vector<Agent> default: " << endl;
sort(t1.begin(), t1.end()); // 1
PrintAgent(t1);
vector<Agent> t2(t);
cout << "vector<Agent> CompareFunc: " << endl;
sort(t2.begin(), t2.end(), CompareFunc); // 2
PrintAgent(t2);
vector<Agent> t3(t);
cout << "vector<Agent> CompareClass(): " << endl;
sort(t3.begin(), t3.end(), CompareClass()); // 3
PrintAgent(t3);
// 使用queue
cout << s << endl;
priority_queue<int> q1; // 默认大根堆
cout << "queue<int> default: " << endl;
q1.push(1000);
q1.push(10000);
q1.push(10);
q1.push(100);
while(!q1.empty()){
cout << q1.top() << "\t";
q1.pop();
}
cout << endl;
priority_queue<int, vector<int>, less<int>> q2; // less == default
cout << "queue<int> less<int>: " << endl;
q2.push(1000);
q2.push(10000);
q2.push(10);
q2.push(100);
while(!q2.empty()){
cout << q2.top() << "\t";
q2.pop();
}
cout << endl;
priority_queue<int, vector<int>, greater<int>> q3; // greater
//priority_queue<int, vector<int>, greater<int>()> q3; // error
cout << "queue<int> greater<int>: " << endl;
q3.push(1000);
q3.push(10000);
q3.push(10);
q3.push(100);
while(!q3.empty()){
cout << q3.top() << "\t";
q3.pop();
}
cout << endl;
priority_queue<Agent> p1; // 1
cout << "queue<Agent> dafault: " << endl;
t_i.a = 1000;
p1.push(t_i);
t_i.a = 100;
p1.push(t_i);
t_i.a = 10;
p1.push(t_i);
t_i.a = 10000;
p1.push(t_i);
while(!p1.empty()){
cout << p1.top().a << "\t";
p1.pop();
}
cout << endl;
//priority_queue<Agent, vector<Agent>, CompareFunc> p2; // 2
cout << "queue<Agent> CompareFunc: error !!" << endl;
priority_queue<Agent, vector<Agent>, CompareClass> p3; // 3
cout << "queue<Agent> CompareClass: " << endl;
t_i.a = 1000;
p3.push(t_i);
t_i.a = 100;
p3.push(t_i);
t_i.a = 10;
p3.push(t_i);
t_i.a = 10000;
p3.push(t_i);
while(!p3.empty()){
cout << p3.top().a << "\t";
p3.pop();
}
cout << endl;
}
结果
总结
1,std::sort(a.begin(), a.end(), compare) 第三个参数compare接受函数名 或者 类对象
比如 :compare_func, less<int>(), compare_class()
2,priority_queue(T, vector<T>, compare) 第三个参数compare接受 类名
比如 : less<int>, compare_class
3,自定义的数据类型可以在内部重载operator<运算符。这样default情况下,就知道排序依据。