Comparador personalizado no conjunto de estruturas
#include <bits/stdc++.h>
using namespace std;
struct Edge {
int a,b,w;
};
bool cmp(const Edge& x, const Edge& y) { return x.w < y.w; }
int main() {
//... preciding code
// declaring set of edge using comparator as a function
set<Edge,bool(*)(const Edge&,const Edge&)> v(cmp);
// another way of declaring set fof edge using comparator as a function
set<Edge,decltype(&cmp)> v(cmp);
for (int i = 0; i < M; ++i) {
int a,b,w; cin >> a >> b >> w;
v.insert({a,b,w});
}
// ... succeeding code
}
// another way using lambda function
auto cmp = [](const Edge& x, const Edge& y) { return x.w < y.w; };
int main() {
// ... preceding code
set<Edge,bool(*)(const Edge&,const Edge&)> v(cmp);
for (int i = 0; i < M; ++i) {
int a,b,w; cin >> a >> b >> w;
v.insert({a,b,w});
}
// ... succeeding code
}
// You can also use the following syntax to declare set v using a lambda
set<Edge,decltype(cmp)> v(cmp);
karrupt