STL-related

This commit is contained in:
e2hang
2025-08-12 23:00:31 +08:00
parent a2e3b28ac0
commit 629c499a1b
9 changed files with 1363 additions and 0 deletions

View File

@@ -0,0 +1,43 @@
#include <queue>
#include <functional>
#include <iostream>
using namespace std;
bool cmp(const int& a, const int& b) {
return a < b;
}
class compare {
public:
template <typename T>
bool operator()(const T& a, const T& b) {
//返回大的,泛型不能直接返回指针,没有初始化
//用struct或者class套一下在class初始化的过程中就顺带着初始化了
return a < b;//false是向上swap
}
};
int main() {
auto cmpl = []<class T>(const T& a, const T& b) {
return a < b;
};
//priority_queue<int, deque<int>, decltype(cmpl)> pq(cmpl);合法
//priority_queue<int, deque<int>, bool(*)(const int&, const int&)> pq(cmp);合法
priority_queue<int, deque<int>, compare> pq;
//上面三种写法非常重要
pq.push(1);
pq.push(2);
pq.push(6);
pq.push(3);
pq.push(7);
pq.push(4);
while (!pq.empty()) {
cout << pq.top() << " ";
pq.pop();
}
cout << endl;
return 0;
}
/*
7 6 4 3 2 1
*/