user3473161
user3473161

Reputation: 327

How can I shuffle a vector with unique pointers in a random sequence with std::shuffle?

I am busy programming a card game. For this card game, I want to shuffle the deck. The deck is a vector of unique pointers and looks something like this:

std::vector<std::unique_ptr<Cards>> _deck;

I tried to get a random sequence with the shuffle function from but I got the following error: "No instance of function template "std::shuffle" matches the argument vector".". View the code block below for the arguments.

std::shuffle(_buildings_deck.begin(), _buildings_deck.back(), std::default_random_engine{});

As far as I can see the arguments that I am using are correct, I still need to choose a random engine but that should not be the problem. So I got to wonder is it possible to use shuffle to order a vector of unique pointers randomly and if so how?

Upvotes: 0

Views: 931

Answers (1)

ivan_onys
ivan_onys

Reputation: 2372

std::shuffle() expects random access iterator as std::vector has. You have to replace std::list with std::vector.

Upvotes: 5

Related Questions