Let's say I want to have a container of Apples that have different prices. I want them sorted by their price always (highest price first), but I also want to quickly retrieve them by their id. What I have so far is the following
struct AppleClass
{
string id;
int price;
bool operator<(const AppleClass& o) const
{
return price > o.price;
}
};
int main()
{
set<AppleClass> myapples;
myapples.insert({"apple1", 500});
myapples.insert({"apple2", 600});
myapples.insert({"apple3", 400});
for (auto& apple : myapples)
{
cout << apple.id << "," << apple.price << endl;
}
}
My app will spend 20% of it's time removing entries, 20% inserting entries, 25% retrieving them (retrieving the whole list), and 35% updating them (their price will increase or decrease) often.
The container will have a maximum of 450 entries.
My code only solves the sort problem. Find is useless since I want to find by their id (so I need to iterate trough all of them). Removing and inserting would be slow for the same reason.
It feels like the wrong choice.
But If I have a map then it would be ordered based on the id. And every time I retrieve the list I would have to copy it to some container for example, order it, and then send it to the user, which also feels slow.
Help!