2013-03-13 78 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) 
}