I used to be using a vector as my card pack which would use a random shuffle function fine, but I recently changed it to an array which has brought up a few problems for me
- It's #include <algorithm> . Don't forget the <angle brackets>.
- Can't tell you if you're calling random_shuffle incorrectly without knowing what s_CardPack is. Can you show us how that is defined?
and highlights line 63
Which line is line 63? I only see 6 lines.
Also, I want to make sure that, for example, if [2][0] was shuffled to [30][0], that [2][1] would also be moved to [30][1].
Is this possible with the random shuffle function or do I have to code a for loop myself?
You should not try to keep multiple different arrays in sync. If you have lots of different information that needs to stay together, put it in a struct, then have an array of that struct:
1 2 3 4 5 6 7 8 9 10 11
struct Card
{
int suit;
int rank;
};
Card deck[52];
// initialize your deck here
random_shuffle(deck, deck + 52);
Oops i did include brackets I just typed that really fast before I pressed enter to avoid getting responses saying that was my problem :P So much for that
static string s_CardPack[52][3]; <-- thats how I defined s_CardPack
When I said it highlights line 63, I meant in the algorithm include file:
#include <bits/stl_algo.h>
that is highlighted with no error to tell me what I am doing wrong.
Anyway I haven't heard of a struct before but it looks like it could save me a lot of time. So does your example mean that creating deck[52] will be a single object and suit and rank will also be arrays? or are you creating 52 different deck "structs" each with their own individual suit and rank?
You need to learn a LOT about iterators.
1. Random_shuffle() accepts iterators as inputs.
2. Vector::begin() and end() return iterators.
3. Iterators are basicly pointers
1.+2.+3. Don't dereference the begin and end of your array when using them as iterators!!!
Use this:
BTW Disch, bits/stl_algo.h is where the actual functions are defined. Algorithm is there just to include some extra needed headers and for the name to be simpler
Awesome, thanks Disch for the info on structs. Looks a lot easier and cleaner to work with
@viliml
I had two dimensions in my array because I hadn't used structs before, and I want more than just a simple "A\3" for my card. I want to be able to compare them with other cards, which is why I had the other dimension to store an integer value and suit in it as well as the face appearance. Now that I know about structs though it looks like I can just use that
Also thanks for the info on iterators. The book never explained why arrays were made into iterators but not vectors, but now I know :P