Ich habe Cmp() -Funktion erstellt, um als Comparator.Aber Fehler zu bekommen. Ich habe diesen Code geschrieben .Es zeigt Fehler:Wie sortiere ich Karte <int, Paar <int,int>> nur nach zweitem Element?
#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;
}
Es gibt Fehler:
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> >'
Gibt es eine andere Art und Weise ohne Verwendung struct Cmp() Funktion?
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.
Sie können eine Karte nicht nach ihren Werten sortieren. Vielleicht haben Sie stattdessen nach einem 'std :: set' gesucht? Wolltest du deinen Schlüssel und deinen Wert umkehren? –
Sie übergeben nicht zur Funktion 'operator()' mit Zeiger, sondern nach Wert. Eine Adressierung wie "a-> second.second" funktioniert nicht. Warum versuchst du nicht, durch Verweis zu gehen? –
ok..Ich gehe für std :: set ... Danke – unknown