[英]Unable to print vector values from map in maps
I am trying to print out the whole contents of map of maps but keep encountering into issues.我正在尝试打印地图 map 的全部内容,但一直遇到问题。 Here is my map initialisation map<int, map<int, vector<int>>> myMap;
这是我的 map 初始化map<int, map<int, vector<int>>> myMap;
I have tried the following code:我尝试了以下代码:
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;
}
As mentioned above, as soon as cit2.second
is used, I got the following error: error: no match for 'operator<<' (operand types are 'std::basic_ostream<char>' and 'const std::vector<int>')|
如上所述,一旦使用cit2.second
,我就会收到以下错误: error: no match for 'operator<<' (operand types are 'std::basic_ostream<char>' and 'const std::vector<int>')|
can someone kindly give me some insights?有人可以给我一些见解吗?
You need to do it like this.你需要这样做。
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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.