std::unordered_set
is implemented as a hash table. Its find
method has an average-case complexity of O(1)
and a worst-case complexity of O(set.size())
key comparisons as specified by [tab:container.hash.req].
By default, std::unordered_set<std::string>
uses operator==
to compare keys, so each key comparison runs in O(str.size())
as specified in [string.view.ops] (operator==(const std::string&, const std::string&)
is defined to be equivalent to std::string_view(str1) == std::string_view(str2)
, which is defined to be equivalent to std::string_view(str1).compare(std::string_view(str2) == 0
).
For an std::unordered_set<std::string>
, the container must calculate a hash of the string to find. By default it uses std::hash<std::string>
for this. The standard doesn't specify any complexity requirements for std::hash<std::string>
, but it's most likely O(str.size())
.