+ 1

What's the issue in custom comparator?

Hi Tried having set of pairs. Can anyone help me understand issue? I think it is due to custom comparator I am using for set of pair, but not able to figure out what issue. https://code.sololearn.com/c26W5z2ex8fu Thanks a lot....!

27th Mar 2022, 5:56 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
11 Answers
+ 2
You simply forgot to pass the comparator to the constructor of the set. Since you are using a function pointer as the template type, the currently used default value is a null pointer, and the code fails at its attempted dereferenciation during a comparison.
27th Mar 2022, 11:15 AM
Shadow
Shadow - avatar
+ 2
The Compare (2nd) argument in set template is a class, whose requirement is that expression comp(a, b), where comp is an object whose type is the class, should be valid (for both non-const and const). So, what you should do is make compare a class, and define the actual comparison in the overloaded (). You should also make it that the function works for both const and non-const object. Define compare as the following will work struct compare { bool operator()(const pair<int,int>& obj1,const pair<int,int>& obj2) const { if (obj1.second != obj2.second) return obj1.second < obj2.second; return obj1.first < obj2.first; } };
27th Mar 2022, 6:41 AM
你知道規則,我也是
你知道規則,我也是 - avatar
+ 1
Good point.. I am using the set as mySet;//(index , count) If I use set as count and index, it works without any requirement of custom comparator. Just wanted to try with custom comparator and it crashed so curious to know issue
27th Mar 2022, 6:33 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
+ 1
Ipang yes, it's is called functor.
27th Mar 2022, 8:37 AM
你知道規則,我也是
你知道規則,我也是 - avatar
+ 1
I thought if function with decltype is not allowed , it should not compile at all... In this case , it gets compiled and results into runtime error.... Are you sure that functor is the only thing we can use even though it compile with function as well And I tried with functor also and it is not working
27th Mar 2022, 10:20 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
+ 1
Also, reply to your code doesn't work with functor. Your operator() is not const.
27th Mar 2022, 10:49 AM
你知道規則,我也是
你知道規則,我也是 - avatar
+ 1
Yeah functor works ...thanks I am still interested to know issue in function with decltype
27th Mar 2022, 10:53 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
0
CarrieForle, Is this how we imitate a function by overloading () operator in a class/struct?
27th Mar 2022, 7:09 AM
Ipang
0
Yeah CarrieForle , I got your point... We can use functor like you said...also lambda should also work.. My concern is why custom comparator as function is not working like I have coded.... What is missing or it is not at all possible ?
27th Mar 2022, 7:29 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
0
I don't think it is. Remind that what set want is a "data type", which is made with keyword class, struct, or enum. Function is not a data type, and the return type of compare (bool) doesn't make a valid expression of bool(a, b).
27th Mar 2022, 8:35 AM
你知道規則,我也是
你知道規則,我也是 - avatar
0
decltype(&function) return the pointer of the return type of the function. decltype doesn't run the function. EDIT: after testing. Yes, decltype(function) does work. My bad. However, declaring a variable of decltype(function) seems doesn't do anything.
27th Mar 2022, 10:27 AM
你知道規則,我也是
你知道規則,我也是 - avatar