NewCodeTemplate
This commit is contained in:
34
模板/其他/比较函数的模板6种.cpp
Normal file
34
模板/其他/比较函数的模板6种.cpp
Normal file
@@ -0,0 +1,34 @@
|
||||
1
|
||||
auto cmp = [&](const Edge& a, const Edge& b) {
|
||||
return a.cost > b.cost; // 小根堆:cost 小的优先
|
||||
};
|
||||
|
||||
priority_queue<Edge, vector<Edge>, decltype(cmp)> pq(cmp);
|
||||
|
||||
2
|
||||
struct Compare {
|
||||
bool operator()(const Edge& a, const Edge& b) const {
|
||||
return a.cost > b.cost; // 小根堆
|
||||
}
|
||||
};
|
||||
|
||||
priority_queue<Edge, vector<Edge>, Compare> pq;
|
||||
|
||||
3
|
||||
using F = function<bool(const Edge&, const Edge&)>;
|
||||
priority_queue<Edge, vector<Edge>, F> pq(
|
||||
[](const Edge& a, const Edge& b){
|
||||
return a.cost > b.cost; // 小根堆
|
||||
}
|
||||
);
|
||||
|
||||
4
|
||||
struct Edge{
|
||||
int from, to, cost;
|
||||
Edge(int f, int t, int c): from(f), to(t), cost(c) {}
|
||||
|
||||
bool operator<(const Edge& other) const {
|
||||
return cost > other.cost; // 小根堆
|
||||
}
|
||||
//一定是<,不能是>
|
||||
}
|
||||
Reference in New Issue
Block a user