12.7. The printDeck function

Whenever you are working with vectors, it is convenient to have a function that prints the contents of the vector. We have seen the pattern for traversing a vector several times, so the following function should be familiar:

void printDeck (const vector<Card>& deck) {
  for (size_t i = 0; i < deck.size(); i++) {
    deck[i].print ();
  }
}

By now it should come as no surprise that we can compose the syntax for vector access with the syntax for invoking a function.

Since deck has type vector<Card>, an element of deck has type Card. Therefore, it is legal to invoke print on deck[i].

A Euchre Deck contains 9’s, 10’s, Jacks, Queens, Kings, and Aces of all four suits. Modify the buildDeck function below to create a Euchre deck. The printDeck function will allow you to verify that you have done this correctly.

Hopefully you took some time to try and figure out the code yourself. The solution below is just one of several correct solutions for creating the Euchre deck:

vector<Card> buildEuchreDeck() {
  vector<Card> deck (24);
  int i = 0;
  for (int suit = 0; suit <= 3; suit++) {
      for (int rank = 1; rank <= 13; rank++) {
        if (rank == 1 || rank >= 9){
          deck[i].suit = suit;
          deck[i].rank = rank;
          i++;
        }
      }
  }
  return deck;
}
You have attempted of activities on this page