结构初始化数组

时间:2010-05-10 21:06:54

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

您好我正在制作一个程序,我必须初始化一副牌。我正在使用结构来表示卡片。然而,当我显示卡片组时,我没有正确地填充它,因为我得到了一堆零。我相信我的错误就在这一行,但我不确定:

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作为我的迭代器...位置是我打算使用的。因为我从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;
    }
}

3 个答案:

答案 0 :(得分:3)

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

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

答案 1 :(得分:1)

以下是一些代码:

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张卡分享套件名称。卡具有-a 套件名称。

也许这会让OO有点太过分了。; - )

答案 2 :(得分:0)

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

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

此外,我建议你把你的牌放在一个指针阵列中。这将使您节省大量的复制操作,因为在这种情况下,每次你将数据索引到一个数组索引时,你正在复制整个结构对象。