如何使用c++在数组中存储不同的值类型
how can I store diffrent value types in an array using c++?
我想在配置类中实现的一些功能遇到了问题,我有一些不同类型的值,我想将它们存储在映射中。以前,我使用ExtendedString类来存储所有数据,并在需要时使用模板函数将它们转换为所需的类型。然后在我的配置类中有一个map<ExtendedString,vector<ExtendedString> >
来将所有密钥存储在配置文件中。以下是我的ExtendedString类的样子:
class ExtendedString : public std::string
{
public:
ExtenedString() : public std::string
{
}
template <class T> ExtenededString(T)
{
std::stringstream s;
*this = s.str();
}
template <class T>T as(){
istringstream s(*this);
T temp;
d >> temp;
return temp;
}
}
现在,我想在加载配置文件时解析所有值,并具有在需要时转换为任何可能类型的功能。例如,如果我将某个值解析为int,并且在运行时我需要将该值作为字符串,我希望能够立即对其进行强制转换。此外,在这种情况下,我不喜欢使用boost,它是我的基类之一,我不想将boost链接到我正在开发的每个项目。
一个经典的解决方案是boost::any
,或大规模的union
。但我认为这两种方法造成的麻烦都多于解决的麻烦。创建一个Config
类,每个配置选项都有成员,这有什么错?
我建议使用boost::any
,因为它听起来像是你想要的。然而,如果你真的不想使用它,你可能可以这样处理它。这可能不是最好的方法(这是我想到的第一件事)——基本上是将值存储在原来的类型中,并作为字符串。如果您尝试将get
作为原始类型,它将返回该值,否则,它将使用std::stringstream
尝试转换它。
注意:这不处理复制/分配,请将d_data
更改为您选择的共享指针来处理。
#include <string>
#include <sstream>
#include <iostream>
class ConfigValue
{
class HolderBase
{
protected:
virtual void writeValueToStream(std::ostream& os) const = 0;
public:
virtual ~HolderBase() { }
template <class Type>
Type getAs() const
{
std::stringstream ss;
writeValueToStream(ss);
Type temp;
ss >> temp;
return temp;
}
};
template <class Type>
class Holder : public HolderBase
{
Type d_value;
protected:
void writeValueToStream(std::ostream& os) const
{
os << d_value;
}
public:
Holder(const Type& value)
: d_value(value)
{
std::ostringstream oss;
oss << value;
}
Type get() const
{
return d_value;
}
};
HolderBase *d_data;
public:
template <class Type>
ConfigValue(const Type& value)
: d_data(new Holder<Type>(value))
{
}
~ConfigValue()
{
delete d_data;
}
template <class Type>
Type get() const
{
if (Holder<Type> *holder = dynamic_cast<Holder<Type>*>(d_data))
{
return holder->get();
}
else
{
return d_data->getAs<Type>();
}
}
};
int main()
{
ConfigValue cv = 10;
std::cout << cv.get<std::string>() << std::endl;
return 0;
}
您可以编写一个"Value"类,然后为每种值编写子类,而不是将它们全部存储为字符串。
相关文章:
- 存储模板类型以强制转换回派生<T>
- 类型总是使用其大小存储在内存中吗
- C++ 将 CIN 值存储到任何类型的数组中
- 哪些存储了不完整类型的 STL 数据结构可以用作类成员?
- 在C++中,如何在变量中正确存储 BYTE 类型?
- 多维数组存储三种不同的数据类型?
- 生成一个类Name_class并将两种数据类型存储在一个向量中
- 在 JavaScript 中,值的数据类型存储在何处以及如何存储?
- 将Protobuf字节类型存储在二进制文件中
- 如何检查程序是否超过数据类型存储
- 将多调数据类型存储到unique_ptr向量中
- C++元编程:按类型存储整数
- 将多个类型存储到同一个容器中
- 如何打印 boost::any 的向量,当我将元素类型存储在另一个字符串向量中时
- 堆栈/堆变量的变量/引用名称或类型存储在内存中的位置
- C++ dynamic_cast vs 将对象类型存储在静态枚举中
- 有没有任何方法可以在运行时将不同的数据类型存储在一个变量中
- 是否可以将Boost uint128_t数据类型存储为原始int类型?
- 使用boost::variant库制作地图.如何以适当的类型存储和显示事物
- 是否可以将对象的类型存储在映射中以进行投射