fork download
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <vector>
  4. #include <string>
  5. #include <random>
  6. #include <algorithm>
  7.  
  8. struct Card
  9. {
  10. enum Suit { Spades, Hearts, Clubs, Diamonds };
  11. enum Face { Ace, Deuce, Three, Four, Five, Six, Seven, Eight, Nine, Ten, Jack, Queen, King };
  12.  
  13. static const unsigned longestFace = 5;
  14.  
  15. Suit suit;
  16. Face face;
  17. };
  18.  
  19. std::string as_string(Card::Suit suit)
  20. {
  21. static const char* suit_str[] = { "Spades", "Hearts", "Clubs", "Diamonds" };
  22. return suit_str[suit];
  23. }
  24.  
  25. std::string as_string(Card::Face face)
  26. {
  27. static const char* face_str[] = {
  28. "Ace", "Deuce", "Three", "Four", "Five", "Six", "Seven",
  29. "Eight", "Nine", "Ten", "Jack", "Queen", "King"
  30. };
  31.  
  32. return face_str[face];
  33. }
  34.  
  35. std::ostream& operator<<(std::ostream& os, Card c)
  36. {
  37. os << std::right << std::setw(Card::longestFace);
  38. os << as_string(c.face) << " of " << as_string(c.suit) ;
  39.  
  40. return os;
  41. }
  42.  
  43. template <typename iter_type>
  44. void printCards(iter_type beg, iter_type end)
  45. {
  46. while (beg != end)
  47. std::cout << *beg++ << '\n';
  48. }
  49.  
  50. int main()
  51. {
  52. std::vector<Card> cards;
  53. for ( unsigned s=0; s<4; ++s)
  54. for (unsigned f = 0; f < 13; ++f)
  55. cards.push_back({ Card::Suit(s), Card::Face(f) });
  56.  
  57. std::mt19937 rng((std::random_device())());
  58.  
  59. for (unsigned runs = 0; runs < 5; ++runs)
  60. {
  61. std::shuffle(cards.begin(), cards.end(), rng);
  62. printCards(cards.begin(), cards.begin() + 5);
  63. std::cout << '\n';
  64. }
  65. }
Success #stdin #stdout 0s 3476KB
stdin
Standard input is empty
stdout
  Ace of Spades
Eight of Spades
Deuce of Spades
  Ace of Hearts
 Five of Hearts

 Nine of Spades
Queen of Diamonds
 King of Clubs
Deuce of Spades
  Ten of Clubs

 Four of Hearts
  Six of Spades
Three of Diamonds
  Ten of Hearts
  Ten of Spades

Queen of Hearts
 Jack of Clubs
Three of Hearts
Deuce of Spades
 King of Hearts

 Nine of Hearts
Three of Hearts
 Jack of Clubs
  Ace of Spades
Queen of Clubs