【问题标题】:go through map c++ [duplicate]通过地图c ++ [重复]
【发布时间】:2013-03-01 20:20:28
【问题描述】:

所以,我有一个std::map<int, my_vector>,我想遍历每个 int 并分析向量。 我还没有开始分析矢量的部分,我仍在试图弄清楚如何遍历地图上的每个元素。 我知道可以有一个迭代器,但我不太明白它是如何工作的,而且我不知道是否有更好的方法来做我打算做的事情

【问题讨论】:

  • This 可能会有所帮助....

标签: c++ map element


【解决方案1】:

您可以简单地遍历地图。每个地图元素都是一个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)
}

【讨论】:

    【解决方案2】:

    迭代器是完美的选择。环顾四周http://www.cplusplus.com/reference/map/map/begin/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-03-25
      • 2016-06-28
      • 2017-10-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-23
      • 1970-01-01
      相关资源
      最近更新 更多