我的程序读取用户的输入并创建简单的“表”。开始时的用户指定列的数据类型和行数。
用户输入:
create table
add attribute string Name
add attribute int Age
rows 3
我现在需要从用户的输入中准备一个结构。我有这样的事情:
CTable
{
unsigned attributesCnt;
string * attributesNames;
void ** attributes;
};
因此,从用户的输入,程序执行以下步骤:
CTable myTable;
myTable.attributesCnt = 2; // string "Name", int "Age"
myTable.attributesNames = new string[2];
myTable.attributesNames[0] = "Name";
myTable.attributesNames[1] = "Age";
attributes = new void[2]; // 2 attributes
attributes[0] = (void*) new string[3]; // there will be 3 rows
attributes[1] = (void*) new int[3];
我需要记住“attributes [0]”是字符串,“attributes [1]”也是int。
这是“正确的”方式吗?
我只想使用标准库。
答案 0 :(得分:1)
您正在寻找的是标记的联合,也称为变体。它允许您将多种数据类型存储在同一位置,就像常规联合一样,但是包含一个指示其类型的附加但独立的数据成员。 C ++标准库不包含变体,但它们很容易实现。
如果您有变体,可以将它应用到您的示例中,如下所示。
myTable.attributesNames[0] = "Name";
myTable.attributesNames[1] = "Age";
// I recommend using std::vector here instead of using new/delete yourself
attributes = new Variant*[2]; // 2 attributes
attributes[0] = new Variant("player name");
attributes[1] = new Variant(player_age);
以下示例显示了如何实现变体。
struct Variant
{
enum Type
{
INT,
STRINGPTR
};
Type type_;
union
{
int int_;
const char* stringptr_;
} data_;
explicit Variant(int data) : type_(INT)
{
data_.int_ = data;
}
explicit Variant(const char *data) : type_(STRINGPTR)
{
data_.stringptr_ = data;
}
Type getType() const { return type_; }
int getIntValue() const
{
if(type_ != INT)
throw std::runtime_error("Variant is not an int");
return data_.int_;
}
const char *getStringPtr() const
{
if(type_ != STRINGPTR)
throw std::runtime_error("Variane is not a string");
return data_.stringptr_;
}
};
int main()
{
Variant intval(1);
Variant stringval("hello");
std::cout << intval.getIntValue() << std::endl;
std::cout << stringval.getStringPtr() << std::endl;
}