What is the real use case for erasing based on std :: unordered_ (set | map)?

It has been found that std::unordered_set and std::unordered_map have an erase(first, last) range based on the range, which, in my opinion, is another good way to shoot in the foot.

Maybe someone knows some real use case for such functions?

Or can this be considered a bad design?

+6
source share
2 answers

OK, I got a response . In C ++ 14, it turns out that elements that are not erased keep the same order, "this allows you to erase individual elements during iteration through the container."

So your iteration will be in some random order. But you can delete all those that you visited in one call. The range to delete will be the same as the range you repeated.

+1
source

I think this is to ensure compatibility with a regular card and install.

But I still find it useful in principle. If the range of values ​​is deleted, you want to remove them from the collection. But the usual use case looks up (a million times more often than insert / delete), and it does not overlap, so the disordered version is good.

How can he shoot you? This is no different from erasing each of them in a loop. Ah, a simple start, the final range of direct traversal will not work, so do you think that the supplied function does not do everything it takes to do everything right, but does the same forward movement that will work on the map, but breaks unordered_map?

Yes, right?

+2
source

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


All Articles