Let's say I want to have a container with apples that has different prices. I want them to be sorted by their price always (first the highest price), but I also want to quickly get them by their identifier. So far, I have been next
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; } }
http://ideone.com/NcjWDZ
My application will spend 20% of time deleting records, 20% inserting records, 25% getting them (getting 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 solves the sorting problem. It is useless to find, since I want to find by their identifier (so I need to repeat all of them). For the same reason, deletion and insertion will be slow.
This seems like the wrong choice.
But if I have a card, then it will be ordered based on the identifier. 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 will also be slow.
Help!
source share