如何在c++中访问映射中的映射.?性能问题

How to access a map within a map in C++ ..? Performance issue?

本文关键字:映射 性能 问题 访问 c++      更新时间:2023-10-16
typedef map<int, string> iMap;
typedef map<double, innerMap> OutMap;
OutMap mx;
map<double, iMap >::iterator it_out;
map<int, string>::iterator it_i;
for ( it_out=mx.begin() ; it_out != mx.end(); it_out++ ) {
   cout << "nnNew elementn" << (*it_out).first << endl;
   for( it_i=(*it_out).second.begin(); it_i != (*it_out).second.end(); it_out++)
     cout << (*it_i).first << " => " << (*it_i).second << endl;
}

我很确定上面的代码是好的…有性能问题吗?

不能再改进了,除了

  • 您可以使用预增量而不是后增量,即++it_out而不是it_out++
  • 可以使用'n'代替endl。使用'n'可以在合理的范围内提高输出操作的性能,因为endl首先将'n'放入输出缓冲区,然后将其刷新到目标(本例中为stdout),从而使其操作缓慢。

注意,应该在内部for循环中增加it_i(而不是it_out)。我想这是一个打字错误。