Tessil / ordered-map

C++ hash map and hash set which preserve the order of insertion
MIT License
512 stars 66 forks source link

The ordered_map is crashed with map.earse(it++) #42

Closed portsip closed 1 year ago

portsip commented 1 year ago

Hi, the below codes cause the application to crash:

tsl::ordered_map<std::string, int32_t> m; m["ABC"] = 1; m["ccc"] = 2;

auto it = m.begin; for (; it != m.end();) { m.erase(it++); // crashe // it = m.erase(it); // this works fine }

Doesn't ordered_map support the m.erase(it++); as well as the std::map?

Tessil commented 1 year ago

"Iterator invalidation behaves in a way closer to std::vector and std::deque (see API for details)", see the https://github.com/Tessil/ordered-map#differences-compared-to-stdunordered_map section.

Your iterator is thus invalidated during the erasure, you can't increment it.