2017-10-28 75 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); 

這就是我插在我的一對 的多集,但我不知道如何打印出我對對 的多集的所有元素我曾嘗試這一點,但它不工作如何打印出對多集的元素

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 /幫助/ MCVE)。 –

回答

0

對面的集迭代(C++ 11系列爲基礎的好這裏):

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

有兩種方法可供選擇。他們幾乎相同,但第二個更短。

第一種方法:

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