【问题标题】:Array of Structs Initialization结构体初始化数组
【发布时间】:2010-05-10 21:06:53
【问题描述】:

您好,我正在开发一个程序,我必须初始化一副纸牌。我正在使用一个结构来表示一张卡片。但是我没有正确填写它,因为当我显示一副牌时我得到一堆零。我相信我的错误在这一行,但我不确定:

struct card temp = {"Clubs", value, false};

代码:

void initCards(){

    int count = 0;
    int location = 0;
    const int hand = 12;

    //add hearts
    int value=2;
    while( count < hand ){
        struct card temp = {"Hearts", value, false};
        cards[location] = temp;
        value++;
        count++;
    }

    count = 0;

    //add diamonts
    value = 2;
    while( count < hand ){
        struct card temp = {"Diamonds", value, false};
        cards[count] = temp;
        value++;
        count++;
    }

    //add spades
    count = 0;
    value = 2;
    while( count < hand ){
        struct card temp = {"Spades", value, false};
        cards[count] = temp;
        value++;
        count++;
    }

    //add clubs
    count = 0;
    value = 2;
    while( count < hand ){
        struct card temp = {"Clubs", value, false};
        cards[count] = temp;
        value++;
        count++;
    }

    //print the deck
    for(int i=0; i<52; i++){
        cout << cards[i].type << " " << cards[i].rank << endl;
    }
}

我不敢相信我正在使用 count 作为我的迭代器... location 是我打算使用的。而且由于我从 2 开始数,手应该是 13。有时你只需要休息一下,然后回来看看错误。这工作正常:

void initCards(){

    int count = 0;
    int location = 0;
    const int hand = 13;

    //add hearts
    int value=2;
    while( count < hand ){
        struct card temp = {"Hearts", value, false};
        cards[location] = temp;
        value++;
        location++;
        count++;
    }

    count = 0;

    //add diamonts
    value = 2;
    while( count < hand ){
        struct card temp = {"Diamonds", value, false};
        cards[location] = temp;
        value++;
        location++;
        count++;
    }

    //add spades
    count = 0;
    value = 2;
    while( count < hand ){
        struct card temp = {"Spades", value, false};
        cards[location] = temp;
        value++;
        location++;
        count++;
    }

    //add clubs
    count = 0;
    value = 2;
    while( count < hand ){
        struct card temp = {"Clubs", value, false};
        cards[location] = temp;
        value++;
        location++;
        count++;
    }


    for(int i=0; i<52; i++){
        cout << cards[i].type << " " << cards[i].rank << endl;
    }
}

【问题讨论】:

  • 代码审查:尝试查看您的代码,看看您是否可以减少重复自己。将来还要使用for 循环,它们更容易发现错误。在编写代码之后,您会停止经常使用while。尝试首先通过每个套件的外部 for 循环。

标签: c++ arrays struct structure new-operator


【解决方案1】:

每次添加新套装时,您都会将 count 重置为零。因此,假设cards 大到足以容纳 52 个元素,它们中的大多数都不会被填充,因为你一开始就一直在写。

如果您发布struct cardcards 的声明,我们将能够更好地为您提供帮助。

【讨论】:

    【解决方案2】:

    这里有一些代码:

    struct Card
    {
      virtual const std::string& get_suite_name(void) const = 0;
      unsigned int value;
    };
    
    struct Spade_Card
    : public Card
    {
        const std::string& get_suite_name(void) const
        {
            static const std::string name = "Spades";
            return name;
        }
    };
    
    struct Heart_Card
    : public Card
    {
        const std::string& get_suite_name(void) const
        {
            static const std::string name = "Hearts";
            return name;
        }
    };
    
    struct Clubs_Card
    : public Card
    {
        const std::string& get_suite_name(void) const
        {
            static const std::string name = "Clubs";
            return name;
        }
    };
    
    #define CARDS_IN_SUITE 13
    #define NUM_SUITES 4
    #define CARDS_IN_DECK ((CARDS_IN_SUITE) * (NUM_SUITES))
    
    int main(void)
    {
        std::vector<Card *> deck;
        // Create the hearts suite & add to the deck.
        unsigned int i = 0;
        for (i = 0; i < CARDS_IN_SUITE; ++i)
        {
            Card * p_card = new Spade_Card;
            if (!p_card)
            {
                cerr << "Error allocating memory for a card." << endl;
                return EXIT_FAILURE;
            }
            p_card->value = i + 1;
            deck.push_back(p_card);
        }
    
        // Repeat for other suites.
    
        for (i = 0; i < CARDS_IN_DECK; ++i)
        {
             delete deck[i];  // Good karma to deallocate memory.
        }
        return EXIT_SUCCESS;
    }
    

    不需要为每张卡重复套件名称。一张卡片与其他 12 张卡片共享一个套件名称。一张卡片有一个套件名称。

    也许这让 OO 有点过头了。 ;-)

    【讨论】:

    • 一团糟……继承……滥用OOD中的概念……我的眼睛着火了!此外,您可以自动创建套装......如果您不依赖于使用继承的方式。一个简单的本地字符串数组,其中包含名称“Hearts”、“Diamonds”、“Clubs”和“Spades”,可以很好地处理这样的循环。当然,保持展开是提高效率的更好主意,但鉴于您的代码,我怀疑是否考虑到了效率。 :P +1 对于这样一个有创意的答案,不过! :D
    【解决方案3】:

    卡片数组总是被最后一个卡片类型覆盖。 您不应将 count 重置为 0,而应检查 while 条件是否小于其值 + 12。

    int temp = count + 12;
    while(count < temp)
    {
    }
    

    此外,我建议您将卡片保存在指针数组中。这将使您节省大量复制操作,因为在这种情况下,每次将卡片分配给数组索引时,您都在复制整个结构对象。

    【讨论】:

    • 不要将指针用于小型数据保存对象,除非您发现复制操作是热点。在这种情况下,复制很可能可以忽略不计。
    猜你喜欢
    • 1970-01-01
    • 2014-02-26
    • 1970-01-01
    • 1970-01-01
    • 2011-03-24
    • 2015-07-29
    • 1970-01-01
    • 1970-01-01
    • 2011-05-09
    相关资源
    最近更新 更多