2013-03-13 75 views
0

所以,我有一個std::map<int, my_vector>,我想通過每個int並分析向量。 我還沒有得到分析矢量的部分,我仍然試圖弄清楚如何通過地圖上的每一個元素。 我知道有可能有一個迭代器,但我不太明白它是如何工作的,而且我不知道是否沒有更好的方法來做我想做的事通過映射C++

+1

[This](http://stackoverflow.com/a/4844904/1410711)可能會有幫助.... – Recker 2013-03-13 18:08:09

回答

6

您可以簡單地迭代地圖。每個地圖元素是std::pair<key, mapped_type>,因此first爲您提供了關鍵元素second

std::map<int, my_vector> m = ....; 
for (std::map<int, my_vector>::const_iterator it = m.begin(); it != m.end(); ++it) 
{ 
    //it-->first gives you the key (int) 
    //it->second gives you the mapped element (vector) 
} 

// C++11 range based for loop 
for (const auto& elem : m) 
{ 
    //elem.first gives you the key (int) 
    //elem.second gives you the mapped element (vector) 
}