Difficulty inserting std :: unordered_multiset

Why is the worst insert complexity std::unordered_multisetlinear? I understand why in this case for std::unordered_set(you need to check that the inserted value is not included in the set), but for the multiset I do not get it. Am I missing something?

+4
source share
1 answer

The worst difficulty for std::unordered_multiset::insert()is linear, because:

  • Unordered associative containers that support non-unique keys are reported to support equivalent keys. When iterating over these containers, elements with equivalent keys are adjacent to each other at iteration, forming groups of equivalent keys.
  • Iterator functions require constant amortized time.

For example, consider the case where 5, 13and 13are inserted into unordered_multiset, which has 4buckets, but unordered_multiset::key_eq(5, 13)returns false. In this case, unordered_multiset::hash_function(5)returns different hash codes for 5and 13. Despite having different hash codes, these items can still be inserted into the same bucket. If the hash function for an integer returns an integer, and the bucket index is the result of a hash module, the number of buckets, then:

  • 5 5, 4 1.
  • 13 13, 4 1.

unordered_set::insert() , unordered_multiset::insert() , . [5, 13] 13, [5, 13, 13]. , size().

, unordered_multiset::insert() , unordered_multiset::rehash() , size(), - . - . size() , , , size(), - O(size()*size()).

+2

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


All Articles