无法从地图中的地图打印矢量值

  • 本文关键字:地图 打印 c++ dictionary
  • 更新时间 :
  • 英文 :


我正试图打印出地图的全部内容,但不断遇到问题。这是我的地图初始化map<int, map<int, vector<int>>> myMap;

我已经尝试了以下代码:

for( auto const & cit : myMap)
{
cout << cit.first << " : ";
auto const & imap = cit.second;
for( auto const & cit2 : imap )
{
cout << cit2.first << ":" << cit2.second << ","; // errors out
//cout << cit2.first << ":"; // works, but it is not printing out the vector<int> portion
}
cout << endl;
}

如上所述,一旦使用cit2.second,我就会得到以下错误:error: no match for 'operator<<' (operand types are 'std::basic_ostream<char>' and 'const std::vector<int>')|

有人能给我一些见解吗?

您需要这样做。

for( auto const & cit : myMap)
{
cout << cit.first << " : ";
auto const & imap = cit.second;
for( auto const & cit2 : imap )
{
cout << cit2.first << ":";
auto const &vec = cit2.second;
for(auto const &i : vec)
{
cout<<i<<" ";
}cout<<endl;   
//cout << cit2.first << ":"; // works, but it is not printing out the vector<int> portion
}
cout << endl;
}

最新更新