The C++Course provides a general introduction to programming in C++. It is based on A.B. Downey's book, How to Think Like a Computer Scientist. Click here for details. 
Home Objects of Vectors Subdecks  
Subdecks
How should we represent a hand or some other subset of a full deck? One easy choice is to make a Deck object that has fewer than 52 cards. We might want a function, subdeck, that takes a vector of cards and a range of indices, and that returns a new vector of cards that contains the specified subset of the deck: Deck Deck::subdeck (int low, int high) const {Deck sub (highlow+1); for (int i = 0; i<sub.cards.length(); i++) { sub.cards[i] = cards[low+i]; } return sub; }
The length of the subdeck is highlow+1 because both the low card and high card are included. This sort of computation can be confusing, and lead to "offbyone" errors. Drawing a picture is usually the best way to avoid them. As an exercise, write a version of findBisect that takes a subdeck as an argument, rather than a deck and an index range. Which version is more errorprone? Which version do you think is more efficient?


Home Objects of Vectors Subdecks 