Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a real use case for std::unordered_(set|map) range based erase?

Tags:

c++

c++11

stl

Found that std::unordered_set and std::unordered_map has range-based erase(first, last) which in my opinion is another nice way to shoot yourself in leg.

Maybe somebody know any real use case for such functionality?

Or this may be considered as bad design?

like image 344
acc15 Avatar asked Nov 15 '14 23:11

acc15


1 Answers

I think it is to provide compatibility with regular map and set.

But I still think it's useful in principle. If a range of values is retired, you want to remove them from the collection. But the normal use case is looking up (a million times more common than insert/delete) and it is not traversed, so the unordered version is good.

How can it shoot you? It's no different from erasing each one in a loop. Ah, the plain start,end range forward traversal won't work, so you think the supplied function doesn't do what it takes to get it right, but does the same forward-range traversal that would work on a map but break on an unordered_map?

Gee, does it?

like image 66
JDługosz Avatar answered Oct 23 '22 03:10

JDługosz