I need FindIngredient to return a const Ingredient & when searching by Ingredient.name. I need to used unordered_set. So I think I need 2 of them one to contain the objects and another one just to contain the string attributes name in order to search by string.
Is my approach ok or can I achieve it using only unordered_set<Ingredient> ingredients?
Following with my approach what how do I return in FindIngredient function? I want to return the object in ingredients whose attribute name is the one found searching in ingredientsByName
class Ingredient {
public:
string Name;
int Price;
string Description;
};
class Pizzeria {
unordered_set<string> ingredientsByName;
unordered_set<Ingredient> ingredients;
public:
void AddIngredient(const string &name, const string &description, const int &price)
{
if(ingredientsByName.find(name) == ingredients.end())
{
throw "Ingredient already inserted";
}
else
{
Ingredient ingredient;
ingredient.Name = name;
ingredient.Price = price;
ingredient.Description = description;
ingredients.insert(ingredient);
ingredientsByName.insert(ingredient.Name);
ingredients.insert(ingredient);
}
}
const Ingredient &FindIngredient(const string &name) const
{
if(ingredientsByName.find(name) == ingredients.end())
{
throw "Ingredient not found";
}
else
{
return // how Do I return the corresponding Ingredient ???
}
}
}