typedef pair<int,int>ii;
vector<ii>vii;
sort(vii.begin(),vii.end(),comp);
ii comp(ii a,ii b){
if(a.first>b.first)
return a;
else if(a.first==b.first){
if(a.second>b.second)
return a;
else
return b;
}
else{
return b;
}
}
//This way it is throwing a compilation error. Can you guide how to sort this vector as per //the conditions given in the compare function.
Presumably you want to sort them lexicographically, in increasing order. You can do this:
std::sort(vii.begin(), vii.end(), std::greater<std::pair<int,int>>());
The comparison functor is a binary predicate, and must return a boolean, and implement strict weak ordering. std::greater<std::pair<int,int>>
does that for you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With