向量迭代器+偏移超出范围,在while循环结束时给出错误

时间:2014-03-12 01:30:44

标签: c++ vector

while (deckSize > 2)
{

    one_Card = card_deck.back();
    card_deck.pop_back();

    two_Card = card_deck.back();
    card_deck.pop_back();

    three_Card = card_deck.back();
    card_deck.pop_back();


    oneCard_Name = card_name(one_Card);
    twoCard_Name = card_name(two_Card);
    threeCard_Name = card_name(three_Card);

    oneCard_Suit = card_suit(one_Card);
    twoCard_Suit = card_suit(two_Card);
    threeCard_Suit = card_suit(three_Card);

    oneCard_Rank = card_rank(one_Card);
    twoCard_Rank = card_rank(two_Card);
    threeCard_Rank = card_rank(three_Card);

    bool between1 = (oneCard_Rank < threeCard_Rank && threeCard_Rank < twoCard_Rank);
    bool between2 = (twoCard_Rank < threeCard_Rank && threeCard_Rank < oneCard_Rank);


    cout << "Here are your two cards: " << endl;
    cout << setw(10) << oneCard_Name << " of " << oneCard_Suit << setw(20) << twoCard_Name << " of " << twoCard_Suit << endl;
    cout << "Do you think the next card will lie between these? (y/n): ";
    cin >> user_input;
    cout << endl << endl;
    cout << "Here is your next card: " << endl;
    cout << setw(10) << threeCard_Name << " of " << threeCard_Suit << endl << endl << endl;
    count++;


    if(user_input == "y" || user_input == "yes" || user_input == "Yes" || user_input == "Y")
    {
        if(between1 || between2)
        {
            cout << "You win!" << endl;
            win++;
        }
        else
        {
            cout << "You lose!" << endl;
            lose++;
        }
    }
    else
    {
        if(between1 || between2)
        {
            cout << "You lose!" << endl;

            lose++;
        }
        else
        {
            cout << "You win!" << endl;

            win++;
        }
    }
}


cout << "You have played this game " << count << " times and you have won: " << win << " and lost " << lose << endl;

return 0;

}

这两个子程序可以随机播放和初始化套牌

void initDeck(vector<int> &card_deck)
{
    int i;
    for(i = 0; i <= 51; i++)
    {
        card_deck[i] = i;
    }

}


void shuffleDeck(vector<int> & card_deck)
{
    int n;

        for(n = 51; n >= 0; n--)
        {
            int i = randomize();
            int temp = card_deck[i];
            int temp2= card_deck[n];
            card_deck[n] = temp;
            card_deck[i] = temp2;

        }

}

当我运行程序后,它允许我运行它,但是当我达到的数字小于while循环中的条件时,它只是给我一个错误,并没有完成程序。我之前遇到过这个错误并修复了它,因此我对错误的含义有了基本的了解。根据我的知识,它试图收集超过矢量长度的数字。但是这次我根本没有看到我的错误。

3 个答案:

答案 0 :(得分:1)

deckSize未在任何地方设置/更新。它应该是card_deck.size()

答案 1 :(得分:0)

您应该使用push_backemplace作为类型向量:

void initDeck(vector<int> &card_deck){
int i;
for(i = 0; i <= 51; i++)
{
    card_deck.push_back(i);
}

}

看看这个link

答案 2 :(得分:0)

试试这个:

void initDeck(vector<int> &card_deck)
{
    int i;
    for(i = 0; i <= 51; i++)
    {
        card_deck.push_back(i);
    }

}


void shuffleDeck(vector<int> & card_deck)
{
    int n;

        for(n = 51; n >= 0; n--)
        {
            int i = randomize();
            int temp = card_deck[i];
            int temp2= card_deck[n];
            card_deck[n] = temp;
            card_deck[i] = temp2;

        }

}

要生成随机数,请参阅thisthis。或者你可以找到更可靠的其他解决方案。