This code performs differently if I add a condition:
First case:
#include<bits/stdc++.h>
using namespace std;
struct comp
{
bool operator()(pair<int,pair<int,int> > a, pair<int,pair<int,int> > b)
{
return a.first>b.first;
}
};
int main()
{
set<pair<int,pair<int,int>>,comp> s;
auto d = s.insert({4,{6,10}});
cout<<(d.first)->first<<" "<<(d.first)->second.first<<" "<<(d.first)->second.second<<endl;
d = s.insert({4,{0,4}});
cout<<(d.first)->first<<" "<<(d.first)->second.first<<" "<<(d.first)->second.second<<endl;
}
Output:
4 6 10
4 6 10
Second case: (with condition on .second)
#include<bits/stdc++.h>
using namespace std;
struct comp
{
bool operator()(pair<int,pair<int,int> > a, pair<int,pair<int,int> > b)
{
if(a.first==b.first)
return a.second.first<b.second.first;
return a.first>b.first;
}
};
int main()
{
set<pair<int,pair<int,int>>,comp> s;
auto d = s.insert({4,{6,10}});
cout<<(d.first)->first<<" "<<(d.first)->second.first<<" "<<(d.first)->second.second<<endl;
d = s.insert({4,{0,4}});
cout<<(d.first)->first<<" "<<(d.first)->second.first<<" "<<(d.first)->second.second<<endl;
}
Output:
4 6 10
4 0 4
Why does the set not add a different pair in the first case? I thought the extra condition only decides the order, and doesn't differentiate between elements.