Attempting to read from a source file to store person objects into a container.
A person object is initialized with a random age, id and a categories vector of five assorted chars.
person.hpp
class person
{
protected :
int age; // 0 - 99
int id; // 20181104 eight numeric characters
std::vector<char> categories = {'A', 'B', 'C', 'D', 'E'}
person()
{
...
std::shuffle(categories.begin(), categories.end(),
random_generator::instance().gen);
}
};
random_generator.hpp
Used to shuffle the person class member categories with std::shuffle
class random_generator
{
public :
static random_generator& instance()
{
static random_generator instance;
return instance;
}
random_generator(random_generator const&) = delete;
random_generator& operator=(random_generator const&) = delete;
private :
random_generator(){}
std::random_device rd;
std::mt19937 gen{rd()};
...
friend class person;
};
person.cpp
std::ostream& operator<<(std::ostream& output, const person& p)
{
output << p.id << ' ' << p.age;
for(char c : p.categories)
output << c << ' ';
return output;
}
std::istream& operator>>(std::istream& input, person& p)
{
input >> p.id >> p.age;
// how can I implement this; suggested alternatives are most welcome
return input;
}
person.txt source file
19850319 33 A C E B D
06111990 28 B E C A D