How to order a set< pair< pair<int, int>, double > > for the second value "double"?

I am super new in C++ and I am really struggling with this: I want to organize my second component of the set:

set< pair <pair<int, int>, double > > map
int N_t = 10, N_r = 10;
for (unsigned int i_t = 0; i_t < N_t; i_t++ )
 {
 for(unsigned int i_r = 0; i_r < N_r; i_r++ )
  {
   Double_t dR = i_t*i_r ;
   map.insert( make_pair(make_pair(i_r, i_t) ,dR));
   }
 }

And I want to organice the second component. I have try this and I dont know how to to it:

sort(map.begin(), map.end(), [](const pair<pair<int,int> &x, double>,const pair<pair<int,int>, double> &y)
{
  return x.second < y.second;
 });

Thanks!!

Upvotes: 0

Views: 1325

Answers (2)

Thanks Benson! I just realize that I have to construct a struct that compares

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

Thanks for the advise!

Upvotes: 0

Benson Lin
Benson Lin

Reputation: 1404

If you wish to sort according to the .second element of the pair, then why not placing the double in front of the pair<int,int>? Sorting orders pairs by their first element then their second element, thus instead of

set< pair <pair<int, int>, double > > map

You could use

set< pair <double, pair<int, int> > > map

Also I suggest not using keywords as variable names (map is an in-built data structure in C++)

Upvotes: 2

Related Questions