2016-01-31 31 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

相關問題