Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to order set<pair<unsigned int, double> > by the second value of pair?

Tags:

c++

set

As the title says, I've built a set of pair of values that I need ordered by the double value (second):

set<pair<unsigned int, double> > s

like image 783
Luis Avatar asked Dec 10 '22 01:12

Luis


1 Answers

You should use the comparator:

struct Cmp
{
    bool operator ()(const pair<unsigned int, double> &a, const pair<unsigned int, double> &b)
    {
        return a.second < b.second;
    }
};

and then you can define your set like:

set <pair<unsigned int, double>, Cmp> your_set;
like image 79
maxteneff Avatar answered Jan 29 '23 16:01

maxteneff