gpt4 book ai didi

c++ - 如何创建具有多个索引的 std::map?

转载 作者:太空宇宙 更新时间:2023-11-04 16:21:50 26 4
gpt4 key购买 nike

以下作品:

std::map<std::string, Animal*> animalMap;

animalMap["KillerRabbit"] = new KillerRabit;

但是如果我想这样做呢?

animalMap["KillerRabbit"]["White"] = new KillerRabit;

我不知道索引括号的“官方”名称是什么,知道它们在谷歌搜索时会有很大帮助 =p

最佳答案

你要找的是 map 中的 map :

std::map<std::string, std::map<std::string, Animal*>> animalMap;

现在 animalMap 中存储的每个值本身就是一个 std::map。外部和内部映射的键类型都是 std::string

[...] 语法是下标运算符。不过,更具体地说,您使用 为 map 下标。键映射到值。

关于c++ - 如何创建具有多个索引的 std::map?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15797266/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com