Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/150.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;具有未知数据类型的结构_C++_Data Structures_Type Conversion - Fatal编程技术网

C++ C++;具有未知数据类型的结构

C++ C++;具有未知数据类型的结构,c++,data-structures,type-conversion,C++,Data Structures,Type Conversion,我的程序读取用户的输入并创建简单的“表格”。开始处的用户指定列的数据类型和行数 用户输入: create table add attribute string Name add attribute int Age rows 3 我需要准备一个结构从用户的输入现在。我有这样的想法: CTable { unsigned attributesCnt; string * attributesNames; void **

我的程序读取用户的输入并创建简单的“表格”。开始处的用户指定列的数据类型和行数

用户输入:

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

这条路“对”吗


我只想使用标准库。

您要查找的是一个标记的联合体,也称为变体。它允许您在同一位置存储多个数据类型,就像常规联合一样,但包含一个额外但独立的数据成员,用于指示其类型。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;
}
结构变量 { 枚举类型 { INT, STRINGPTR }; 类型\类型; 联盟 { int_; const char*stringptr; }数据采集; 显式变量(int数据):类型(int) { data_uu.int_uu=数据; } 显式变量(const char*data):类型(STRINGPTR) { 数据u.stringptr_u=数据; } 类型getType()常量{返回类型} int getIntValue()常量 { 如果(类型为\!=INT) 抛出std::runtime_错误(“Variant不是int”); 返回数据int; } 常量char*getStringPtr()常量 { if(type_!=STRINGPTR) 抛出std::runtime_错误(“Variane不是字符串”); 返回数据u.stringptr u; } }; int main() { 变体intval(1); 变体stringval(“你好”);
std::你是否需要这样的东西。问问你自己:一个空的大小是多少?@uberwulu:谢谢你的评论。我使用一个指向空的指针,指针的大小取决于平台。这是真的吗?转发我不知道属性的数量和它们的类型。我需要struct,我在哪里可以存储“2 int,3 string”或“1 int,1 string”,或者“1整数,2字符,3字符串,1双精度".@CaptainObvlious:谢谢。我正在寻找使用标准库的解决方案。Captain Unforvious使您走上了正确的道路,您也可以从中受益。谢谢,就是这样:-)!不客气。我仍然建议您至少看看
boost::variant
boost::any
。它们是一个更完整的实现.