简体   繁体   English

C ++-改组对象向量

[英]C++ - Shuffling a vector of objects

I am trying to make a deck of cards, I have a vector of card objects but when I use my shuffle function and print the deck they are all in the same order as they were when they were made. 我试图制作一副纸牌,我有一副纸牌对象,但是当我使用随机播放功能并打印纸牌时,它们的顺序与制作时的顺序相同。

Here's my playingCards class: 这是我的playingCards类:

#include <iostream>
#include <string>
#include <random>
#include <vector>
#include <algorithm>

using namespace std;

class playingCards {
private:
    struct card {
        char suit;
        char value;
    };

    const int deckSize = 52;
    vector<card> deck;

public:
    playingCards();
    void printDeck();
    void shuffle();
};

playingCards::playingCards() {
    deck.reserve(deckSize);

    for (int i = 0; i < deckSize; i++) {
        int setSuit = i % 4;

        if      (setSuit == 0) deck[i].suit = 'D';
        else if (setSuit == 1) deck[i].suit = 'H';
        else if (setSuit == 2) deck[i].suit = 'S';
        else if (setSuit == 3) deck[i].suit = 'C';

        int setValue = i % 13;

        if      (setValue == 0)  deck[i].value = 'A';
        else if (setValue == 9)  deck[i].value = 'T';
        else if (setValue == 10) deck[i].value = 'J';
        else if (setValue == 11) deck[i].value = 'Q';
        else if (setValue == 12) deck[i].value = 'K';
        else                     deck[i].value = '0' + (setValue + 1);
    }
}

void playingCards::printDeck() {
    for (int i = 0; i < deckSize; i++) {
        cout << deck[i].value << deck[i].suit << endl;
    }
}

void playingCards::shuffle() {
    auto rng = default_random_engine {};

    std::shuffle(begin(deck), end(deck), rng);
}

I've also tried to pass by reference my vector into the shuffle function like so: 我也试图通过引用将我的向量传递给shuffle函数,如下所示:

void playingCards::shuffle(vector<card>& deck) {
    auto rng = default_random_engine {};

    std::shuffle(begin(deck), end(deck), rng);
}

This didn't work either. 这也不起作用。 Any help would be appreciated, I'm new to using vectors so I really don't understand what could be happening here. 任何帮助将不胜感激,我是使用向量的新手,所以我真的不明白这里可能发生什么。

deck.reserve(deckSize);

That doesn't add any elements to your vector, it only reserves space for them. 不会在向量中添加任何元素,只会为其保留空间。 So the size of your vector is zero, and when you shuffle it, nothing happens. 因此,向量的大小为零,并且在对其进行随机排序时,什么也没有发生。 The rest of your constructor, which sets (non-existent) elements of the vector, as well as your printDeck function, are both undefined behavior. 构造函数的其余部分(设置(不存在)向量的元素)以及printDeck函数都是未定义的行为。 Use resize instead. 请改用resize

deck.resize(deckSize);

In fact, it would be better to just remove the deckSize variable altogether, and just use deck.size() . 实际上,最好完全删除deckSize变量,而仅使用deck.size()会更好。

You have to use 你必须用

deck.emplace_back(value)

in order to create the actual vector. 为了创建实际的矢量。 You just reserved the allocated memory which does not instantiate the vector. 您只是保留了不实例化向量的已分配内存。 Beware of using deck[i] , this can cause undefined behavior, instead, use deck.at(i) . 提防使用deck[i] ,这可能会导致不确定的行为,请改用deck.at(i) For this specific question, use deck.emplace_back(value) instead of deck[i]. 对于此特定问题,请使用deck.emplace_back(value)而不是deck[i].

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM