Sign in to follow this  
dan1088352

template question

Recommended Posts

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).

Share this post


Link to post
Share on other sites
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 type

int main()
{
card deck[51];

srand(time(0)); // initialize the RNG.
std::random_shuffle(card, card+51); // shuffle
}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this