0

我用 C++ 制作了这个非常简单的纸牌。牌组是随机洗牌的,我拿一张牌,然后按 Ace、One、Two、...、Ten、Jack、Queen、King 的顺序发音。如果这张牌正是我所说的,我会从牌组中取出这张牌。我使用剩余的牌组迭代该过程,直到我移除卡片或直到牌组为空(仅在这种情况下我赢了)。这是我编写的代码,但是当我使用功能擦除删除卡时,第 62 行出现错误。我不明白是哪个问题。

#include <iostream>
#include <vector>
using namespace std;

enum SUIT { HEART, CLUB, DIAMOND, SPADE };
string suit_string[] = {"HEART", "CLUB", "DIAMOND", "SPADE"};

enum VALUE { ACE, TWO, THREE, FOUR, FIVE, SIX,
    SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING};
string value_string[] = {"ACE", "TWO", "THREE", "FOUR", "FIVE",
    "SIX", "SEVEN", "EIGHT","NINE", "TEN", "JACK", "QUEEN", "KING"};

class Card {
 public:
    Card(SUIT s, VALUE v);
    SUIT suit;
    VALUE value;
    string toString();
};

Card::Card(SUIT s, VALUE v){
    suit=s;
    value=v;
}

string Card::toString(){
    return value_string[(int)this->value]+" of "+suit_string[(int)this->suit];
}



int main() {
        vector<Card*> deck;
        for (int i=0; i<4; i++) {
            for (int j=0; j<13; j++) {
                deck.push_back(new Card((SUIT)i,(VALUE)j));
            }               
        }

        int removed_count, runs = 0;

        do{
            random_shuffle(deck.begin(),deck.end());
            removed_count=0;

            cout<<"--- Run number: "<<++runs<<"\n";
            cout<<"--- still "<<deck.size()<<" cards in the deck\n";

            for (int i=0; i<4; i++) {
                for (int j=0; j<13; j++) {
                    //cout<<i*13+j<<"\n";

                    int position=i*13+j; //from 0 to 51

                    cout<<"'"+value_string[j]+"'"<<" <-> ";

                    Card card=*(deck.at(position));
                    cout<<card.toString()<<" ";

                        if(card.value == j){
                            removed_count++;
                            deck.erase(deck.begin()+position); //problems here!
                            cout<<"removed\n";
                        }else{
                            cout<<"not removed\n";
                        }

                    }               
            }
            if(deck.empty()) break;
        }while(removed_count>0);

        if(deck.empty()){
            cout<<"You win!\n";
        }else{
            cout<<"You lose!\n";
        }

        return 0;
}
4

1 回答 1

4

好吧,当我启动它时,我得到

'KING' <-> terminate called after throwing an instance of 'std::out_of_range'
  what():  vector::_M_range_check

显然,这意味着您正在尝试访问超出向量范围的内容。您正在擦除循环中的一个元素:

deck.erase(deck.begin()+position);

命中后,向量的大小将是 51 而不是 52。但是,您的循环正在处理 52 个值,正如您在此评论中明确提到的:

int position=i*13+j; //from 0 to 51

所以在物品被移除并且牌组中还剩下 51 个物品(0 到 50)之后,最后一次迭代仍然会尝试访问第 51 个元素并崩溃。它失败的实际行是这一行:

Card card=*(deck.at(position));

当状态如下: 在此处输入图像描述

如您所见,向量中有 51 个元素,*(deck.at(position))将尝试访问第 52 个元素(索引为 51,因为您从零开始计数),并抛出异常。

于 2012-09-28T12:11:37.013 回答