I am making a simple card game, trying to shuffle a deck of cards that i made with a map.

std::map<int, std::string> theDeck;

How could I go about shuffling the items in the deck? I am having issues figuring out the algorithm. This is what I tried, but didn't work.

void shuffleDeck(std::map<int, std::string> & theDeck)
	std::map<int, std::string>::iterator it = theDeck.begin();
	for (int i = 0; i < 51; i++)
		int r = i + (rand() % (52-i)); // Random Remaining position
		int tempInt = it->first;
		std::string tempString = it->second;
		theDeck[i] = theDeck[r];
		theDeck[r] = it->second;
6 Years
Discussion Span
Last Post by MasterGberry

The random_shuffle isn't compatible with the std::map, from all the error messages I am getting at least.


I don't think its possible to shuffle std::map because it doesn't make any sense to do that. Seems to me what you should do to represent a deck of cards is a vector of integers, so that 101 = Ace of Hearts, 102 = 2 of hearts, 201 = ace of diamonds, 301 is ace of clubs and 401 is ace of spades. Then you could use std::ramdom_shuffle() to mix them up.


Hmm, what my program does with this map is it assigns the values of 0-51 to the 52 cards and uses the string part to give the integers actual names. So i am trying to shuffle the pairs around.


Yes I realize that, but you can't shuffle std::map -- the order is fixed by the class. What I proposed was just about the same thing except use cards in the range 100-152 as Hearts, 201-252 as Diamonds, etc.

Edited by Ancient Dragon: n/a


You can't shuffle map. A Quote from C++ Reference

Internally, map containers keep their elements ordered by their keys from lower to higher , therefore begin returns the element with the lowest key value in the map

so you just made a poor choice in data structure. I suggest you to use arrays of structs.


If you're using consecutive integers for the map indices, why not just use a vector?

Independently of that, it would seem to me that you could simply replace lines 8 through 11 of your code by a call to std::swap(theDeck, theDeck[r]) and delete line 4 entirely.


Well what do you suggest as the best container for a deck of cards?


Its a poker game.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.