gpt4 book ai didi

c++ - 使用 C++ 的具有一种类型键但具有不同类型值的复杂映射。 (不是 multimap )

转载 作者:行者123 更新时间:2023-11-28 02:46:03 24 4
gpt4 key购买 nike

我想制作一张这样的 map ,

1) 总共 50 个字符串键。2) 前 40 个值是字符串。3) 其他10个值为整数。

喜欢

//No idea how to initialize this map but after initialization
map2D["Key-1"]="Value-1";
map2D["Key-2"]="Value-2";
map2D["Key-3"]="Value-3";
........................
........................
........................
map2D["Key-39"]="Value-39";
map2D["Key-41"]= 123;
map2D["Key-42"]=234;
.......................
.......................
.......................
map2d["Key-50"]=24132;

知道如何实现这个目标。谢谢

最佳答案

您可以使用 boost 库中的类变体。将其视为非 POD 类型的 union 。

http://www.boost.org/doc/libs/1_55_0/doc/html/variant.html

这是网页中的示例:

std::vector< boost::variant<int, std::string> > vec;
vec.push_back( 21 );
vec.push_back( "hello " );

因此,您的容器的值类型可以是 boost::variant<int, std::string> .

关于c++ - 使用 C++ 的具有一种类型键但具有不同类型值的复杂映射。 (不是 multimap ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24404276/

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