This question is language-agnostic, but I'm specifically looking for a solution using C++ STL containers. I have a struct like this.
struct User {
int query_count;
std::string user_id;
}
std::multiset<User> users; //currently using
I use a multiset with a comparator that sort on query_count. This allow me to have sorted multiple entries with the same query_count. Now, if I want to avoid duplicates on user_id, I need to scan data and remove the entry and create a new one, taking O(n). I'm trying to think of a way to do this in sub-linear time. I was thinking of a solution based on a map ordered on user_id, but then I would have to scan all the whole data when trying to locate the largest query_count.
EDIT: requirements are insert, delete, update(delete/insert), get highest query_count, find user_id in sub-linear time.
I prefer to use the standard stl containers, but simple modifications are fine. Is there any way to achieve my requirements?
Summary:
The summary of the answers is that to use a ootb solution, I can use boost bi-directional map.
If I'm sticking to STL, then it has to be a combination of using two maps together, updating both carefully, for each insertion of a user.