-2

I have created Cmp() function to use as a Comparator.But Iam getting error. I have written this code .It shows error:

#include<bits/stdc++.h>
using namespace std;
struct cmp
    {
        bool operator() (multimap<int,pair<int,int> > a, multimap<int,pair<int,int> > b)
            {
                if(a->second.second>b->second.second)
                    return 1;
                return 0;
            }
    };
int main()
{
    std::ios::sync_with_stdio(false);
    int test,i;
    long long sum=0;
    cin>>test;
    while(test--)
    {
        multimap<int, pair<int,int>,cmp > mymap;
        multimap<int, pair<int,int> >::iterator it;
        int n,days,d,t,s;
        cin>>n>>days;
        for(i=0;i<n;i++)
        {
            cin>>d>>t>>s;
            mymap.insert(make_pair(d,make_pair(t,s)));
        }
        for(it=mymap.begin();it!=mymap.end();it++)
        {
            cout<<it->first<<"  "<<it->second.first<<" "<<it->second.second<<endl;
        }

    }
    return 0;
}

It gives error :

In member function 'bool cmp::operator()(std::multimap >, std::multimap >)':

 [Error] base operand of '->' has non-pointer type 'std::multimap<int, 
std::pair<int, int> >'

Is there any other way without using struct Cmp() function?

eg:- suppose i have

(3,(2,300))
(3,(1,400))
(3,(2,500))
(2,(3,100))
(2,(2,500))
(1,(5,100))

I want output like this:
(1,(5,100))
(2,(2,500))
(2,(3,100))
(3,(2,500))
(3,(1,400))
(3,(2,300))

   Only the second element of pair<int,int> sorted decreasingly.
Baum mit Augen
  • 49,044
  • 25
  • 144
  • 182
  • 1
    You can't sort a map by it's values. Perhaps you were looking for a `std::set` instead? Did you mean to invert your key and your value? – François Andrieux Jul 10 '17 at 17:28
  • You are not passing to function `operator()` by pointer but by value. So addressing like `a->second.second` doesn't work. Why don't you try to pass by reference. – Marek Vitek Jul 10 '17 at 17:32
  • ok..Iam going for std::set...Thanks –  Jul 10 '17 at 17:40
  • `` is (a) not a standard file, and (b) is a bad practice because using it you don't get to learn to individual includes for the various bits of the standard library. See https://stackoverflow.com/questions/25311011/how-does-include-bits-stdc-h-work-in-c – iksemyonov Jul 10 '17 at 19:16

1 Answers1

1

The basis of the question doesn't make sense. From cppreference:

The order of the key-value pairs whose keys compare equivalent is the order of insertion and does not change.

You can't dictate the order of the values. If you need them sorted, you'll need to either copy them into a new container first or put them in a new container to begin with.

Also, the Compare type compares the keys, not the entire map. There are examples in the linked reference as well.

Barry
  • 286,269
  • 29
  • 621
  • 977