2017-07-10 12 views
-2

コンパイラとして使用するCmp()関数を作成しましたが、エラーが発生しています。私はこのコードを書かれている .ITは示しエラー:マップを並べ替える方法<int、pair <int,int>>は2番目の要素のみに従っていますか?

#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; 
} 

それが与えるエラー:

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> >' 

構造体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. 
+1

マップを値でソートすることはできません。代わりに 'std :: set'を探していたのでしょうか?あなたの鍵とあなたの価値を逆転させることを意味しましたか? –

+0

ポインタではなく、値で 'operator()'関数に渡しています。したがって、 'a-> second.second'のようなアドレス指定は機能しません。なぜあなたは参照渡ししようとしないのですか? –

+0

ok ..私はstd :: setに向いています...ありがとう – unknown

答えて

1

質問の根拠は合理的ではありません。 cppreferenceから:

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

あなたはの順序を決定することはできません。並べ替えが必要な場合は、最初にそれらを新しいコンテナにコピーするか、新しいコンテナに配置する必要があります。

また、Compareタイプはキー、ないマップ全体を比較します。リンクされた参照にも例があります。

関連する問題