打印地图矢量

时间:2013-03-07 00:23:57

标签: c++

如何打印多图的矢量? 例如,我有一个看起来像这样的矢量:

typedef std::multimap<double,std::string> resultMap;
typedef std::vector<resultMap> vector_results;

修改

for(vector_results::iterator i = vector_maps.begin(); i!= vector_maps.end(); i++)
{
   for(vector_results::iterator j = i->first.begin(); j!= i->second.end(); j++)
   {
      std::cout << j->first << " " << j->second <<std::endl;
   }
}

1 个答案:

答案 0 :(得分:1)

外部i循环中的for变量指向resultMap,这意味着j变量的类型必须为resultMap::iterator。将您的循环重写为

for(vector_results::const_iterator i = vector_maps.begin(); i != vector_maps.end(); ++i)
{
   for(resultMap::const_iterator j = i->begin(); j != i->end(); ++j)
   {
      std::cout << j->first << " " << j->second << std::endl;
   }
}

我将迭代器类型更改为const_iterator,因为迭代器没有修改容器元素。


如果您的编译器支持基于C ++ 11的基于范围的for循环,则可以更简洁地编写代码

for( auto const& i : vector_maps ) {
  for( auto const& j : i ) {
    std::cout << j.first << " " << j.second << std::endl;
  }
}
相关问题