Given two std::sets, one can simply iterate through both sets simultaneously and compare the elements, resulting in linear complexity. This doesn’t work for std::unordered_sets, because the elements may be stored in any order. So how expensive is a == b for std::unordered_set?
Given two std::set s, one can simply iterate through both sets simultaneously and compare
Share
Complexity of
operator==andoperator!=:Linear complexity in the average case. N2 in the worst case, where N is the size of the container.
More details in the standard §23.2.5, point 11:
For
unordered_setandunordered_map, the complexity ofoperator==(i.e., the number of calls to the==operator of thevalue_type, to the predicate returned bykey_equal(), and to the hasher returned byhash_function()) is proportional toNin the average case and to N2 in the worst case, whereNisa.size().