【发布时间】:2011-12-01 12:31:44
【问题描述】:
假设我有以下用户结构:
struct User {
string userId;
UserType userType; // UserType is just an enumeration
string hostName;
string ipAddress;
//and more other attributes will be added here
};
我需要存储一组用户记录(大约 10^5 个用户,也可以扩大规模)。如果我将其存储为 unordered_set 或 unordered_map,性能会更好吗? Unordered_set 在技术上与 HashSet 相同,而 unordered_map 与 HashMap 相同,对吧?使用常规集合(有序)不是一种选择,因为当元素数量增加时插入和删除会变得非常慢。
unordered_set <User> userRecords;
或
unordered_map <string, User> userRecords; // string is the user ID.
我需要它在插入、删除和通过 userId 访问特定用户对象方面非常快。
【问题讨论】:
标签: c++ data-structures unordered-map unordered-set