【发布时间】:2013-05-26 19:45:23
【问题描述】:
我的程序读取用户的输入并创建简单的“表格”。用户在开始时指定列的数据类型和行数。
用户输入:
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。
这是“正确”的方式吗?
我只想使用标准库。
【问题讨论】:
-
你需要像
boost::variant这样的东西。 -
问问自己:空隙的大小是多少?
-
@uberwulu:感谢您的评论。我使用指向 void 的指针,指针的大小取决于平台。这是真的吗?转发我不知道属性的数量和它们的类型。我需要 struct,我在哪里可以存储“2 int, 3 string”或“1 int, 1 string”或“1 int, 2 char, 3 string, 1 double”。
-
@CaptainObvlious:谢谢。我正在寻找使用标准库的解决方案。
-
Captain oblivious 带你走上正轨,看
boost::any你也可能受益
标签: c++ data-structures type-conversion