// Map created
std::map<int, std::vector<int>> _map;
// Key/Data inserted
_map.insert(std::pair<int, std::vector<int> >(0, { i }));
// Display values [ERROR]
for (const auto &p : _map)
{
std::cout << "m[" << p.first << "] = " << p.second << '\n';
}
这是一个非常简单的程序,用于创建 map 、插入值并通过遍历整个 map 来显示键/对。 我能够显示 map 键 (p.first) 但无法显示数据值 (p.second)。
Error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'const std::vector>' (or there is no acceptable conversion)
最佳答案
p.second
在std::vector<int>
并且标准库不会重载 operator<<
对于 vector
s,如错误消息所述。所以你必须添加一个内部循环来遍历 vector
并打印它包含的值。
而不是...
std::cout << "m[" << p.first << "] = " << p.second << '\n';
...尝试...
std::cout << "m[" << p.first << "] = {";
for (int n : p.second)
std::cout << ' ' << n;
std::cout << " }\n";
关于c++ - 无法显示 map 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31333404/