如何给嵌套映射赋值
how assign values to a nested map
我有一个地图嵌套在另一个地图,我想分配值给外部地图,但我不太确定如何做到这一点。这使得程序在开始之前就达到收支平衡。当我通过
运行它时没有显示任何错误map<int, map<int, int>> outer;
map<int, int> inner;
outer.emplace(1, make_pair(2, 1));
outer.emplace(2, make_pair(2, 1));
outer.emplace(3, make_pair(2, 1));
outer.emplace(1, make_pair(3, 1));
嗯,您的外部映射的mapped_type是map<int, int>
,但您正试图用pair<int, int>
构建它。你可以试试
outer.emplace(1, map<int,int>{ { 2, 1 } });
outer.emplace(2, map<int,int>{ { 2, 1 } });
outer.emplace(3, map<int,int>{ { 2, 1 } });
outer.emplace(1, map<int,int>{ { 3, 1 } });
的缺点是它很难看,它甚至可能不是你想要的:最后一行没有效果,因为已经有一个键1
的值,在这种情况下放置没有效果。如果您打算将条目{ 3, 1 }
添加到第一个内部映射中,这样它现在就包含了{ { 2, 1 }, { 3, 1 } }
,那么您可以使用以下结构,这看起来要好得多:
outer[1].emplace(2, 1);
outer[2].emplace(2, 1);
outer[3].emplace(2, 1);
outer[1].emplace(3, 1);
相关文章:
- 为"adjacent"变量赋值时出现问题
- C++中的赋值发生,尽管右侧出现异常
- 用C++中的sscanf赋值
- 为std::string的某个索引赋值
- 重载Singly Linked List中的赋值运算符
- 为什么我必须在C++中添加一个赋值符号来声明一个数组
- gtest_使用setargpointee在函数中赋值
- 非常量变量只读位置的赋值
- 正在将无序映射设置为无序映射的值
- 使用赋值运算符重载从类中返回jobject
- C++数据文件、数组和计算赋值
- 由于没有使用赋值运算符,映射的值是如何初始化的?
- 通过迭代器为映射的成员赋值
- C++空映射赋值:等于vs插入
- C++ std::映射分配错误:无法在赋值中将' type **'转换为'type *'
- 从映射中替换赋值的宏
- 带有映射的赋值操作符
- 如何给嵌套映射赋值
- 给结构体映射赋值的语法
- c++ 11 STL在两个映射之间使用move赋值合并