【发布时间】:2017-03-21 22:31:10
【问题描述】:
我有 3 个不同的 C++ 映射声明。
map<string, string> map1;
map<string, int> map2;
map<string, double> map3;
我想根据情况使用其中一张地图。比方说
if (map_value == "map1") { default_map = map1; }
else if (map_value == "map2") { default_map = map2; }
else { default_map = map3; }
我的问题是我不知道如何在 C++ 中声明 default_map 可以根据条件进行更改。我期待 C++ 中的 map<Object,Object> default_map 声明。
编辑。
我选择string、int 和double 作为假设对象。实际的map1、map2 和map3 存储函数指针。根据地图中的值,我调用不同的函数。函数签名是相同的,但那些函数指针来自不同的类。本质上,class A 有void funcA(string);,class B 有void funcA(string);,class C 有void funcA(string);,funcA 的实现方式因类而异。
感谢任何帮助。
【问题讨论】:
-
您打算如何使用
default_map? -
换句话说,你会如何处理你假设的
Object? -
variant<std::map<std::string, std::string>, std::map<std::string, int>, std::map<std::string, double>>? -
@Kerrek SB 我选择
string, int, and double作为假设对象。实际的 map1、map2 和 map3 存储函数指针。根据地图中的值,我调用不同的函数。 -
@Jarod42 我不确定如何使用 Variant。我可以将
default_map声明为变体并将default_map分配给map1, map2 or map3吗?
标签: c++ dictionary stl stdmap