Saving items in unordered_set and saving them in unordered_map

Suppose I have the following user structure:

struct User { string userId; UserType userType; // UserType is just an enumeration string hostName; string ipAddress; //and more other attributes will be added here }; 

and I need to save a collection of user records (about 10 ^ 5 users, can also scale). Would it be better in performance if I store it as an unordered_set or unordered_map? Unordered_set is technically the same as a HashSet, and unordered_map is the same as HashMap, right? Using a regular set (ordered) is not an option, as inserting and deleting will be very slow when the number of elements increases.

 unordered_set <User> userRecords; 

OR

 unordered_map <string, User> userRecords; // string is the user ID. 

I need it to be very fast in terms of inserting, deleting and accessing a specific user object of its userId.

+6
source share
3 answers

I would choose unordered_map , because I can get the user, given the user ID at any time, without extra work, and with unordered_set I do not have this tool.

As for the mentioned operations, the speed will be almost the same.

+7
source

Since unordered_set<> does not allow you to easily access a user using his userId, unordered_map<> seems to be the right choice.

+6
source

If performance is a serious issue, you probably want to browse the profile and see which one works best. Otherwise, choose the one that most logically describes what you are trying to do. [Only for 100K elements, I think set and map can have acceptable performance if you need to order somewhere else]

+6
source

Source: https://habr.com/ru/post/898674/


All Articles