2017-10-28 77 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