sort
sort默认从小到大排序
对结构体排序必须重载小于号 – 调试代码中row-25必须重载 “<”
大根堆
大根堆默认堆顶为最大值
对大根堆内结构体排序必须重载小于号(与名字相反) – 调试代码中row-25必须重载 “<”
小根堆
小根堆默认堆顶为最小值
对小根堆内结构体排序必须重载大于号(与名字相反 – 调试代码中row-25必须重载 “>”当第row-27与row-25一致时:排序是和默认排序方法一致的排序;row-27与row-25不一致时则相反:
sort: 从小到大
大根堆:堆顶最大
小根堆:堆顶最小
调试代码:
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
struct Node
{
int a, b;
bool operator < (const Node& w) const
{
return a < w.a;
}
}node[5];
int main()
{
for(int i = 0; i < 5; i ++) node[i] = {4 - i, i};
puts("原序列:");
for(auto x : node) cout << x.a << " ";
puts("");
puts("重载小于号---:");
puts("sort:");
sort(node, node + 5);
for(auto x : node) cout << x.a << " ";
puts("");
//大根堆要重载小于号。
puts("大根堆:");
priority_queue<Node> q;
for(int i = 0; i < 5; i ++) q.push(node[i]);
while(q.size()) cout << q.top().a << " ", q.pop();
puts("");
//小根堆要重载小于号
// puts("小根堆:");
// priority_queue<Node, vector<Node>, greater<Node>> q2;
// for(int i = 0; i < 5; i ++) q2.push(node[i]);
// while(q2.size()) cout << q2.top().a << " ", q2.pop();
puts("");
return 0;
}