J'ai créé la fonction Cmp() à utiliser en tant que Comparator.But Iam obtenir une erreur. J'ai écrit ce code .It indique l'erreur:Comment faire pour trier la carte <int, la paire <int,int>> selon le deuxième élément seulement?
#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;
}
Il donne l'erreur:
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> >'
est-il une autre façon sans utiliser la fonction struct Cmp()?
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.
Vous ne pouvez pas trier une carte en fonction de ses valeurs. Peut-être cherchiez-vous un 'std :: set' à la place? Vouliez-vous inverser votre clé et votre valeur? –
Vous ne passez pas à la fonction 'operator()' par un pointeur mais par une valeur. Donc adresser comme 'a-> second.second' ne fonctionne pas. Pourquoi n'essayez-vous pas de passer par référence. –
ok..Iam va pour std :: set ... Merci – unknown