c++ - 在 C++ 中的 3D map 中插入值

标签 c++ dictionary

我是 C++ map 新手,有一张像这样的 3D map

map<int,map<const char*,const char*>>Map3D;

我想以这样一种方式插入值:当两个 const char* 值都超过 50 时,int 值会递增。我正在跟踪 const char* 值。谁能告诉我如何在此 map 中插入值。我正在做这样的事情

Map3D.insert(pair<int,map<const char* ,const char*>(count,pair<const char*,const char*>(TempA,TempB)));

但它不起作用。

编辑

std::map<int,map<const char*,const char*>>::iterator it= Map3D.begin();
std::map<const char*, const char*>::iterator sub_it = subMap.begin();

最佳答案

您缺少 >,并且您不能递归地将 std::pair 插入到内部映射中,您必须创建一个新映射或使用一个现有的

  map<int,map<const char*,const char*>> Map3D;
  map<const char*,const char*> subMap;

  const char hello[] = "hello";
  const char world[] = "world";

  subMap.insert(std::pair<const char*,const char*>(hello, world));
  Map3D.insert(std::pair<int,map<const char*,const char*>>(22, subMap));

编辑:要回读元素,请查看 http://www.cplusplus.com/reference/map/map/operator[]/

  map<const char*,const char*> subMapCopy = Map3D[22];
  cout << subMapCopy[hello]; // world

编辑 II:使用迭代器:http://www.cplusplus.com/reference/map/map/begin/

 std::map<int,map<const char*,const char*>>::iterator it= Map3D.begin();
 std::map<const char*, const char*>::iterator sub_it = subMap.begin();
 cout << it->second[sub_it->first]; // world

关于c++ - 在 C++ 中的 3D map 中插入值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24361862/

相关文章:

c++ - 列出所有 N 位数字,包含 [string] 个数字

C++ 重载映射 [ ] 运算符

javascript - 以编程方式为移动应用程序获取靠近纬度和经度的多个街道名称

java - 如何修改一个字段的一个公共(public)字段...如link.next.next.next(.next*n) n次

c++ - 如何在C++中获取网页的源代码?

c++ - 尝试销毁 sf::Font 时出现段错误

python - 向字典添加键和值

Python - 轻松将文本文件内容转换为字典值/键

c++ - Valgrind 提示 string.append(string)

c++ - `(i & (i + 1)) - 1` 是什么意思? (在芬威克树中)