template question
can the paramaters of a func be an aray? this is for a card dealing function, card[51] is what needs to be shuffeled.
Pass it as a pointer.
void bar(){ card deck[] = new deck[52]; shuffle(deck, 52);}void shuffle(card* deck, int sizeOfArray){ deck[index] = 0; // where index < sizeOfArray.}
Quote:Original post by dan1088352
can the paramaters of a func be an aray? this is for a card dealing function, card[51] is what needs to be shuffeled.
Since you mention templating, you can actually save yourself from having to explicitly pass the size:
#include <cstddef>template< ::std::size_t ArrayLength >void shuffle( card (& deck)[ ArrayLength ] ){ // Do stuff}int main(){ card deck[51]; // ... shuffle( deck );}
This, of course, will only work with an array whose size data is known at compile-time (IE usually a non-dynamically allocated array).
Quote:Original post by dan1088352
can the paramaters of a func be an aray? this is for a card dealing function, card[51] is what needs to be shuffeled.
You only have 51 cards? Ok.
#include <cstdlib> // For srand()#include <ctime> // For time()#include <algorithm> // For std::random_shuffle()#include "card.h" // Hypothetical header for your card typeint main(){ card deck[51]; srand(time(0)); // initialize the RNG. std::random_shuffle(card, card+51); // shuffle}
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement