1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
|
#include <iostream>
#include <vector>
#include <random>
#include <algorithm>
enum Animal { Rat, Ox, Tiger, Rabbit, Dragon, Snake, Horse, Sheep, Monkey, Rooster, Dog, Pig };
constexpr std::size_t NUM_ANIMALS = Pig+1 ;
std::ostream& operator<< ( std::ostream& stm, Animal a )
{
static const std::string names[NUM_ANIMALS] = { "Rat", "Ox", "Tiger", "Rabbit",
"Dragon", "Snake", "Horse", "Sheep", "Monkey", "Rooster", "Dog", "Pig" };
return stm << names[a] ;
}
std::vector<Animal> construct_set( std::size_t sz )
{
static Animal all_animals[] = { Rat, Ox, Tiger, Rabbit, Dragon, Snake, Horse, Sheep, Monkey, Rooster, Dog, Pig };
static std::mt19937 rng( std::random_device{}() ) ; // random number generator
if( sz > NUM_ANIMALS ) sz = NUM_ANIMALS ;
std::shuffle( all_animals, all_animals+NUM_ANIMALS, rng ) ;
return { all_animals, all_animals+sz } ;
}
void construct_set( Animal* array, std::size_t sz )
{
const auto set = construct_set(sz) ;
std::copy( set.begin(), set.end(), array ) ;
}
int main()
{
for( int i = 0 ; i < 5 ; ++i )
{
std::cout << '#' << i << ". [" ;
for( Animal a : construct_set(8) ) std::cout << ' ' << a ;
std::cout << " ]\n" ;
}
Animal array[5] {} ;
construct_set( array, sizeof(array) / sizeof(Animal) ) ;
std::cout << "\nthe array contains: [ " ;
for( Animal a : array ) std::cout << a << ' ' ;
std::cout << "]\n" ;
}
|