Is it possible to remove elements from an std::unordered_set through bucket iterators?
Asked Answered
S

1

9

Like the question says, can you remove an element from a std::unordered_set using a bucket iterator (local_iterator)? I can see two possible solutions:

  • Since erase() does only accept global iterators, is there equivalent functionality for local_iterator?
  • Is it possible to obtain the equivalent global iterator for a local_iterator?

If it's not feasible, please elaborate on why it's not.

Sabu answered 29/8, 2013 at 7:34 Comment(6)
You can't use both it and ++it in the same function call.Intemerate
Updated, still results in the same error however.Zendah
You should read up on std::remove_if. It doesn't actually remove elements.Almira
Thanks, I didn't know that. I removed the whole example since it's no longer of any use.Zendah
I don't see a technical reason why it wouldn't be possible to construct a global iterator from a local iterator and its corresponding bucket index, but I don't see way to do that with standard functions.Almira
My solution to this problem was to convert local_iterator to iterator using unordered_set.find(*local_iterator)Consumer
A
5

The obvious answer is no, since there is no function in the interface which supports this. There is also no way to get to an iterator from a local_iterator, for the obvious reason that a local_iterator contains a lot less information. (For most implementations, I suspect that it would be fairly simple to implement erase( local_iterator ), had the standard required it. On the other hand, I can't think of a conceivable use for it, which may be why the standard didn't require it.)

Auspice answered 29/8, 2013 at 8:15 Comment(3)
I doubt both "obvious" arguments. (1) What other then bucket number would be needed to turn a local_iterator into an iterator? (2) There are lots of use-cases to erase local_iterator when iterating over a bucket -- just like there are use-cases to erase iterators when iterating over the full container. Without erase for local_iterator, is there even any difference between std::unordered_set's const_local_iterator and local_iterator?Chipboard
You cant think of a reason. But obviously the person that asked the question has.Sake
...I can't think of a conceivable use for it... - I need to erase by bucket iterator to implement an efficient LRU cache, where I want to avoid duplicating a potentially large composite key in the linked list used for tracking LRU order.Clifford

© 2022 - 2024 — McMap. All rights reserved.