正如前面的答案正确建议的那样,您不能在 C++ 中开箱即用。我假设通过“[...]可以接受任何类型的值[...]”你指的是地图的值,而不是键。
不过,这是您可以做的。你有两个选择;我要从丑变美
第一种方法:
创建一个您将用作的值持有类value
对于地图。我们就这样称呼它吧Value.
在该类中为您想要支持的所有类型实现显式构造函数,并跟踪该类当前存储的值的类型
从映射中获取值后检查值的类型并使用适当的 getter 函数
可选地,超载<<
支持标准流的运算符
有关示例实现,请参阅以下代码:
#include <iostream>
#include <memory>
#include <map>
class Value {
public:
typedef enum {
String,
Integer,
Double,
Float
} ContentType;
private:
ContentType m_ctType;
std::string m_strContent;
int m_nContent;
double m_dContent;
float m_fContent;
public:
Value() : m_strContent(""), m_ctType(String) {}
explicit Value(const char* arrcString) : m_strContent(std::string(arrcString)), m_ctType(String) {}
explicit Value(std::string strContent) : m_strContent(strContent), m_ctType(String) {}
explicit Value(int nContent) : m_nContent(nContent), m_ctType(Integer) {}
explicit Value(double dContent) : m_dContent(dContent), m_ctType(Double) {}
explicit Value(float fContent) : m_fContent(fContent), m_ctType(Float) {}
~Value() {}
ContentType type() {
return m_ctType;
}
std::string stringValue() { return m_strContent; }
int integerValue() { return m_nContent; }
double doubleValue() { return m_dContent; }
float floatValue() { return m_fContent; }
};
std::ostream& operator<<(std::ostream& osStream, Value& valOut) {
switch(valOut.type()) {
case Value::String: osStream << valOut.stringValue(); break;
case Value::Integer: osStream << valOut.integerValue(); break;
case Value::Double: osStream << valOut.doubleValue(); break;
case Value::Float: osStream << valOut.floatValue(); break;
}
return osStream;
}
这可以像这样使用:
int main() {
std::map<int, Value> mapAnyValue;
mapAnyValue[0] = Value("Test");
mapAnyValue[1] = Value(1337);
std::cout << mapAnyValue[0] << ", " << mapAnyValue[1] << std::endl;
return 0;
}
这输出
Test, 1337
现在有些人可能会说这是
- 效率低下(它为不在每个中使用的类型保留了字段
Value
实例)
- 难以扩展/维护(添加新字段有点麻烦)
- 一般来说糟糕的设计
他们是对的。因此,这是使用多态性和模板的替代方案。
第二种方法:
这要求您在将值分配给变量时定义要存储的值的类型,并且需要使用指针。原因如下。
对于这种方法,我们执行以下操作:
创建基类ValueBase
作为一个类,我们可以将其放入地图中value type.
从该类派生模板类Value<T>
保存模板类型的任意值T
.
支持std::cout
和朋友们,我们实现了一个运算符重载<<
上课ValueBase
,添加纯虚拟output
功能为ValueBase
,并重写此函数Value<T>
使用默认值<<
您在模板中使用的任何类型的运算符。
请参阅下面的代码示例:
#include <iostream>
#include <memory>
#include <map>
class ValueBase {
public:
ValueBase() {}
~ValueBase() {}
virtual void output(std::ostream& osStream) = 0;
};
template<typename T>
class Value : public ValueBase {
private:
T m_tValue;
public:
Value(T tValue) : m_tValue(tValue) {}
~Value() {}
T value() {
return m_tValue;
}
void output(std::ostream& osStream) override {
osStream << m_tValue;
}
};
std::ostream& operator<<(std::ostream& osStream, ValueBase& valOut) {
valOut.output(osStream);
return osStream;
}
这可以像这样使用:
int main() {
std::map<int, std::shared_ptr<ValueBase>> mapAnyValue;
mapAnyValue[0] = std::make_shared<Value<std::string>>("Test");
mapAnyValue[1] = std::make_shared<Value<int>>(1337);
std::cout << *mapAnyValue[0] << ", " << *mapAnyValue[1] << std::endl;
return 0;
}
或者没有智能指针:
int main() {
std::map<int, ValueBase*> mapAnyValue;
mapAnyValue[0] = new Value<std::string>("Test");
mapAnyValue[1] = new Value<int>(1337);
std::cout << *mapAnyValue[0] << ", " << *mapAnyValue[1] << std::endl;
delete mapAnyValue[0];
delete mapAnyValue[1];
return 0;
}
均输出
Test, 1337
第二种方法在使用方面存在一些差异。
首先,您需要使用指针。这样做的原因是,这样一来,成员函数vtable被保留,并且您可以从派生类重写基类中的函数。在我们的情况下,这意味着:当我们调用output()
在类型指针上ValueBase
被初始化为Value<T>
, the output()
函数来自Value<T>
被使用而不是来自ValueBase
。如果您使用普通变量而不是指针,output()
函数来自ValueBase
将被使用,并且我们会丢失派生类的信息。
其次,这与第一个相关,您需要引用使用该值时获得的指针。如果你想输出一个ValueBase
or Value<T>
指针与std::cout
,你需要这样做std::cout << *var
输出包含的值。如果你刚刚这样做了std::cout << var
,您将正确地获得指针的地址。
我确信还有其他选择,特别是在使用时Boost,但我不是这方面的专家。其他人可能有更有价值的信息。
除此之外,你所做的事情听起来像是一种懒惰的行为。 C++ 拥有强类型系统是有原因的;它不仅定义良好,而且您还知道对代码的期望。如果你开始让事情变得模糊并使用任意容器对象来完成各种任务,你的代码将失去可读性、清晰度,并且(很可能)会产生大量难以跟踪、调试和最终修复的错误,因为您需要支持您引入的所有精美容器以保持框架运行。
如果你想使用Java这样的语言,最好使用Java而不是C++。