Search
 
SCRIPT & CODE EXAMPLE
 

CPP

custom comparator in set of struct

#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);
Comment

PREVIOUS NEXT
Code Example
Cpp :: border radius layout android xml 
Cpp :: c++ display numbers as binary 
Cpp :: c++ srand() 
Cpp :: write variable to file cpp 
Cpp :: c++ in linux 
Cpp :: c++ remove whitespace from string and keep the same size 
Cpp :: check variable type c++ 
Cpp :: length of 2d array c++ 
Cpp :: finding no of unique characters in a string c++ 
Cpp :: finding size of columns and rows in 2d vector c++ 
Cpp :: qt double en qstring 
Cpp :: Modulo Exponentiaon,Iteratve Modulo Exponentiation 
Cpp :: function as argument in another function in c++ 
Cpp :: helloworld in c++ 
Cpp :: fork c 
Cpp :: c++ random 
Cpp :: to_string c++ 
Cpp :: c++ product of vector 
Cpp :: how to iterater map of sets in c++ 
Cpp :: Appending a vector to a vector in C++ 
Cpp :: c++ iterate over vector 
Cpp :: C++ switch cases 
Cpp :: set precision with fixed c++ 
Cpp :: c++ return multiple values 
Cpp :: read text from file c++ 
Cpp :: vector.find() 
Cpp :: include cpp 
Cpp :: docker.io : Depends: containerd (= 1.2.6-0ubuntu1~) E: Unable to correct problems, you have held broken packages 
Cpp :: coordinate in 1d array c++ 
Cpp :: calloc c++ 
ADD CONTENT
Topic
Content
Source link
Name
3+3 =