• Advertisement
Sign in to follow this  

template question

This topic is 4846 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

can the paramaters of a func be an aray? this is for a card dealing function, card[51] is what needs to be shuffeled.

Share this post


Link to post
Share on other sites
Advertisement
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.
}

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.

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
Sign in to follow this  

  • Advertisement