c++ - Change key in unordered multimap -
is there efficient way alter key in unordered multimap? example, want alter key key_val1
key_val2
. looks can done much efficiently iterating on equal_range(...)
, readding pairs new key subsequent erasing old one.
no. if want alter keys, have little selection delete existing item, create new item new key, , insert new item set.
an unordered_multi(set|map)
(at to the lowest degree normally) implemented hash table. alter in key requires computing hash new key (which may entirely different hash old key, though key modified).
thinking things, can if you're ambitious enough. i'm not @ sure it's worthwhile, possible. in particular, unordered associative containers back upwards called local iterators
. local iterator can iterate through items in specific bucket in table. definition, items equal keys must in same bucket.
so, you'd start getting local iterator bucket containing items. you'd iterate through using key_equal
find items contains have same key. you'd utilize hasher find new bucket of those, , straight move them new bucket.
i've never done this, i'm not certain local_iterator gives plenty access it--but seem @ to the lowest degree might possible--likely plenty worth farther investigation, anyway.
my own take on things if you're plenty bother optimizing task, worth considering using unordered_map<key, std::vector<whatever>>
, you'd have 1 stored key, arbitrary number of items associated it. if need modify key, moving associated info trivial (happens pretty much automatically).
c++ c++11 multimap
No comments:
Post a Comment