c++ - 无法显示 map 数据

标签 c++ dictionary vector

// 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.secondstd::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/

相关文章:

java - Java 中的字符串与 Map 中的键进行比较

java - 如何自动将用户定义的对象存储到 map 中?

Java 基础 : create class object

c++ - 从 C++ 中的类的函数成员返回数组

c++ - 是否可以判断哪个数组元素比较结果为真?

python - 使用 BeautifulSoup 迭代列表

c++ - C++中信息树的设计问题

r - 如何连接向量?

c++ - 无法在二维矩阵( vector 的 vector )中设置值

c++ - 查找未排序的 vector<int>