#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define int long long
const int N = 1e5 + 20;
int m[N];
struct comp {
bool operator() (const pair <int , int> &lhs, const pair <int , int> &rhs) const {
return lhs.first > rhs.first;
}
};
int32_t main()
{
ios_base :: sync_with_stdio(0);
cin.tie(0);
set <pair <int , int> , comp> s;
s.insert({1 , 1});
s.insert({1 , 2});
s.insert({1 , 3});
s.insert({1 , 4});
for(auto x : s)
cout << "(" << x.first << "," << x.second << ") ";
cout << endl;
return 0;
}
I expected the above code to print : (1,4) (1,3) (1,2) (1,1) But it only prints :
(1,1)
However, that works fine without the comparator. Why is this strange thing happening?
Auto comment: topic has been updated by wannbegood (previous revision, new revision, compare).
All elements in a set must be unique when compared using the comparator.
The comparator considers two elements
a
andb
equal ifa < b == false
andb < a == false
In your case all elements are considered equal by the set since only the first element of each pair is compared a simple fix would be
Thanks, that helped.
Pandemic Can you please suggest errors/fixes for this problem as well : Comparators in C++
i don't know how to maintain the relative order and move zeros to the end using sort.
you can use use to move zeros to the end and sort the rest of the elements in non-decreasing order using this
I found that my comparator doesn't follow the transitivity rule that every comparator should follow, that is,
if(comp(a , b) == true and comp(b, c) == true)
, thencomp(a , c) should also be true
.std::pair
already defines all comparison operators in such a way that both elements are checked. So you can just writelhs > rhs
. Even shorter would be to get rid ofcomp
altogether and usestd::greater<>
instead.That's the risk of using a comparator with set. Basically what a comparator does is that, it treats the elements you're inserting, as the quantity that is being compared to insert it. In this case, you're comparing only the first value to insert in the set which are all the same, and as set doesn't contain repititions, it is discarding all the "1" values other than the first one.
For example:
Thanks for sharing!
anshumankr Can you please suggest errors/fixes for this problem as well : Comparators in C++
i just want to know one thing that by using this comparator is our execution time increased or remains almost same