Один из способов - сохранить данные в std::vector
:
class Drink
{
unsigned int id;
std::string name;
double price;
friend std::istream& operator>>(std::istream& input, Drink& d);
};
std::istream& operator>>(std::istream& input, Drink& d)
{
input >> d.id;
input >> d.name;
input >> d.price;
return input;
}
Ваш код ввода будет выглядеть так:
std::ifstream drink_file("drinks.txt");
std::vector<Drink> database;
Drink d;
while (drink_file >> d)
{
database.push_back(d);
}
Вы можете найти в database
напиток с ID == 2:
size_t quantity = database.size();
for (size_t index = 0; index < quantity; ++index)
{
if (database[index].id == 2)
{
// Do something with record ID 2.
break;
}
}