2017-10-28 6 views
1
multiset< pair<int,pair<int,int>> >ml; 
pair<int,pair<int,int>> p; 
p.first=3; 
p.second.first=5; 
p.second.second=2; 
ml.insert(p); 

それが、それだけで(C++ 11の範囲ベースのためには、ここで良いです)セットにわたって反復どのように私はペア のペアの私のマルチセットに挿入しかし、私は、私が試してみましたペア のペアの私のマルチセット内のすべての要素をプリントアウトする方法がわからない方法です

multiset< pair<long long,pair<long long,long long> > >::iterator it; 
     it=ml.begin(); 
    p=*it; 
cout<<p.first<<" "<<p.second.first<<" "<<p.second.second<<endl; 
+1

あなたを持っている何試した?あなたの試みはどうやって働いたのですか? [良い質問をする方法を読む](http://stackoverflow.com/help/how-to-ask)を読んで、[最小限の完全で検証可能な例](http:// stackoverflow。 com/help/mcve)。 –

答えて

0

を働いていない:

for (auto x : ml) 
{ 
    cout << "First: " << x.first <<" " << " Second first: " << x.second.first << " Second.second: " << x.second.second << endl; 
} 
0

これには2つのアプローチがあります。彼らはほとんど同じですが、2番目のものははるかに短いです。

最初のアプローチ:

for (multiset< pair<int, pair<int,int> > >::iterator it = ml.begin(); it!=ml.end(); it++) { 
    cout<<"First: "<<it->first<<", Second: "<<it->second.first<<", Third: "<<it->second.second<<endl; 
} 

第二のアプローチ(だけでは、後にC++ 11と):

for (auto it:ml) { 
    cout<<"First: "<<it.first<<", Second: "<<it.second.first<<", Third: "<<it.second.second<<endl; 
} 

と出力は同じです。

First: 3, Second: 5, Third: 2 
関連する問題