所以,我有一个std::map<int, my_vector>
,我想通过每个int并分析向量.我还没有完成分析矢量的部分,我还在试图弄清楚如何浏览地图上的每个元素.我知道有可能有一个迭代器,但我不太明白它是如何工作的,而且,我不知道是否有更好的方法来做我打算做的事情
您可以简单地遍历地图.每个map元素都是一个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)
}
Run Code Online (Sandbox Code Playgroud)