为什么当我调用告诉它的方法时,我的 deque 不会pop_front?

Why my deque won't pop_front when I call the method that tells it to?

本文关键字:deque 我的 不会 pop front 方法 调用 为什么      更新时间:2023-10-16

所以,我有一个项目,我的一个类方法应该pop_front deque,mydeque。当我在 main 中调用 one.play() 并运行程序时,它不会弹出 deque。该程序应该让用户输入他们想要的卡数,然后在每次抽卡时生成一个随机数并比较卡上的值。如果随机数小于卡号,则分数递增 1。但是为了做到这一点,我需要弹出双面的正面,并将新数字与每次双面的新正面进行比较。

#include <iostream>
#include <deque>
using namespace std;
class Player {
    private:

            size_t cards;
            deque<int> mydeque;
    public:
            string name;
            int score;
            Player(string player) {
                    name = player;
                    score = 0;
            }
            void recieve(size_t card){
               mydeque.push_front (card);
            }
            int play() {
                    return mydeque.front();
                    mydeque.pop_front();
            }
            ~Player() {
                    name = " ";
                    score = 0;
            }
            void tostring(ostream & out) const{
                    out << "player name : "<< name << endl;
                    out << "score : " << score << endl;
                    out << "cards : " << mydeque.size() << endl;
                    for(int i =0; i <mydeque.size();i++)
                            out << mydeque[i] << " " ;                       
            }
};
            ostream & operator <<(ostream & out, const Player & p){
                    p.tostring(out);
                    return out;}
int main () {
    int rounds;
    cout << "Give the number of rounds: "<<endl;
    cin >> rounds;
    Player one("some player");
    for (int i = 1; i < rounds+1; i++){
            one.recieve(i);
    }

    for( int i = 0; i < rounds; i++) {
            one.play();
            int randnum = rand()%(rounds-1 + 1) + 1;
            cout << one << endl;
            cout << " The dealer draws : " << randnum << endl;
 if (randnum < one.play()){
                    one.score = one.score + 1;}}

    return 0;
}

return语句执行后,函数结束。

改变:

int play() {
    return mydeque.front();
    mydeque.pop_front();
}

自:

int play() {
    int x = mydeque.front();
    mydeque.pop_front();
    return x;
}