C ++关联数组任意类型的值 [英] C++ associative array with arbitrary types for values
本文介绍了C ++关联数组任意类型的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
什么是有任意值类型的关联数组在C每个键+?
What is the best way to have an associative array with arbitrary value types for each key in C++?
目前我的计划是创建我会期待类型的成员变量价值类。例如:
Currently my plan is to create a "value" class with member variables of the types I will be expecting. For example:
class Value {
int iValue;
Value(int v) { iValue = v; }
std::string sValue;
Value(std::string v) { sValue = v; }
SomeClass *cValue;
Value(SomeClass *v) { cValue = c; }
};
std::map<std::string, Value> table;
这个一个缺点是你必须访问值时,知道类型。即:
A downside with this is you have to know the type when accessing the "Value". i.e.:
table["something"] = Value(5);
SomeClass *s = table["something"].cValue; // broken pointer
还有更多的类型被放在值越臃肿的阵列会。
Also the more types that are put in Value, the more bloated the array will be.
什么更好的建议?
推荐答案
提振::变种看来你是寻找什么。
这篇关于C ++关联数组任意类型的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文