2016-01-31 33 views
0

这是我正在使用的数据类型。如何循环两对STL集<pair<t1,t2>,对<t1,t2>>中的所有元素?

set< std::pair<string,string>,std::pair<string,string>> foo; 

这是我在超过它的循环

for(auto &e: foo){ 
    cout << e.first << " " << e.second // this is where I am having an issue. 
} 

是否有可能使用自动这种方式失败的尝试? e.g

e.first, e.second // some c++ magic (i realize -> is wrong) In pseudo -> // e.third ... 

我喜欢使用汽车,但如果不是我会怎么写我使用的数据类型的迭代器?

+0

但你*的*已经循环虽然集。问题是,在你的循环中,“e.first”是*** ***对***,“e.second”是数据***对***。 –

+2

从什么时候'std :: set'有键和值?你是不是指'map'而不是'set'? – Rumburak

回答

1

你正在做的事情非常奇怪。

集的签名是:

template< 
    class Key, 
    class Compare = std::less<Key>, 
    class Allocator = std::allocator<Key> 
> class set; 

所以,你正在使用std::pair<std::string, std::string>作为比较。只要您尝试插入某些内容,它将无法编译。

但我很确定那不是你想要的。

你可能要么需要

map<pair<string, string>, pair<string, string>>; 

set<pair<pair<string, string>, pair<string<string>>>; 

也许

set<tuple<string, string, string, string>>; 
+0

谢谢。我最终需要。 'set ,pair >> foo'然后我可以使用'cout << item1.first <<“”<< item1.second << "->“<< item2.first <<“”<< item2.second << endl;' – user136952

相关问题