martinus / unordered_dense

A fast & densely stored hashmap and hashset based on robin-hood backward shift deletion
MIT License
898 stars 72 forks source link

[BUG] erase operation dosen't work for iterator #50

Closed stillcold closed 1 year ago

stillcold commented 1 year ago

Hi guys,

It seams erase operation dosen't work for iterator.

I put my code here on compiler exploer.

As we can see, the code will not work in line 53, which should behavior just the same as line 25 does.

Please help me out.

Thanks in advance!

martinus commented 1 year ago

iterators are NOT stable. In line 42 you write ret = umap.insert(mypair);, and right after you insert something int the map with umap["b"] = "c";. This invalidates the iterator, you are not allowed to use ret any more.

stillcold commented 1 year ago

I see. Thanks very much!

When I use robin_hood::unordered_node_map instead of ankerl::unordered_dense::map, it is also fails.

Is there any work aound about this case?

martinus commented 1 year ago

No, iterators are not stable. It's also not stable for std::unordered_map when it rehashes. It's simply not allowed to store iterators. Use pointers with std::unordered_map instead.