![](/img/trans.png)
[英]Get the key and values of map elements in a vector of maps from a constant iterator to the vector of maps
[英]Unable to print vector values from map in maps
我正在嘗試打印地圖 map 的全部內容,但一直遇到問題。 這是我的 map 初始化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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.