-1

I am trying to sort vector of pairs by second element.But it is not working instead it is showing stl_algo.h file and showing error. Here is the code below. Please ignore my bits header and using namespace std.And I have already read this > How do I sort a vector of pairs based on the second element of the pair?

#include<bits/stdc++.h>
using namespace std;

struct sort_pred
{
    bool comp(pair< pair<int,int> , double >&l, pair< pair<int,int> , double >&r)  
    {
        return l.second<r.second;
    }
};


int main()
{
     vector< pair< pair<int,int> ,double > >arr;
     int n;
     scanf("%d",&n);
     int w,v;
     pair< pair<int,int> ,double >temp;
     for(int i=0;i<n;i++)
     {
         scanf("%d%d",&w,&v);
         temp.first.first=w;
         temp.first.second=v;
         temp.second=w*1.00/v;
         arr.push_back(temp);
     }
     sort(arr.begin(),arr.end(),sort_pred());
     for(int i=0;i<n;i++)
     {
         printf("%d %d  %f\n",arr[i].first.first,arr[i].first.second,arr[i].second);
     }
     return 0;
 }
Community
  • 1
  • 1
Shauqi
  • 95
  • 2
  • 7

2 Answers2

1
struct sort_pred
{
    bool comp(pair< pair<int,int> , double >&l, pair< pair<int,int> , double >&r)  
    {
        return l.second<r.second;
    }
};

should be

struct sort_pred
{
    bool operator()(pair<pair<int,int>, double> const &l, pair<pair<int,int>, double> const &r)  
    {
        return l.second<r.second;
    }
};

The 3rd argument of std::sort must be a callable. To make a struct callable you need to define the operator()

bolov
  • 72,283
  • 15
  • 145
  • 224
1

If you use std::sort you have either to implement a compare function ( bool func( A, B ) ):

bool sort_pred_func( const pair<pair<int,int>,double> &l, const pair<pair<int,int>,double> &r )  
{
   return l.second < r.second;
}

sort( arr.begin(), arr.end(), sort_pred_func );

or a comperator class, with method bool operator()( A, B );

struct sort_pred
{
    bool operator()( const pair<pair<int,int>,double> &l, const pair<pair<int,int>,double> &r )  
    {
        return l.second < r.second;
    }

};

sort(arr.begin(),arr.end(),sort_pred());

But you can`t mix it as you did.

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • Sir i used your second method but still it is giving error opening a file naming stl_algo.h and my complier is giving error showing line message 2289 error: no match for call to '(sort_pred) (std::pair – Shauqi Jan 11 '16 at 16:58
  • Sir i am using codeblocks 13.12 for ide and i have enabled c++11. – Shauqi Jan 11 '16 at 17:06
  • Thank you very much sir it is working. I have called function by value and i t worked. Sir can you tell me why call by reference was not working?? – Shauqi Jan 11 '16 at 17:09
  • That article is about `std::sort` taking its comparator by reference, not about the comparator taking its arguments by reference. There is no reason the comparator shouldn't take its arguments by reference. They should be const references, but that is not necessarily required. Unfortunately, the OP truncating his error messages doesn't help much in solving his problem. – Benjamin Lindley Jan 11 '16 at 17:25