Unordered_set: delete with move

In C ++ 11, the std :: unordered_set container provides both insert overload and the new emplace function, so it can be used with non-copyable construct keys, for example std :: unique_ptr.

What happens if you want to delete one of these keys? Is it auto temp = std::move(*some_iterator)valid? Is there any function that allows us to erase an element and at the same time move it to the pace?

Edit: I tried to keep it short, sweet and simple, but to be more clear:

  • Is there an iterator adapter (maybe move_iterator?) That will allow me to move an element from the container and remove that iterator?
  • If not, why not? Should future C ++ include such an interface?

The situation seems impossible: you cannot cancel the key before deleting it, and after deleting it, you cannot gain access.

+4
source share
2 answers

For std::unordered_set<T>an element, emplace()moving objects is unnecessary: ​​either emplace()fixed objects in a container.

: std::move() std::unordered_set<T>, const const . erase() a std::unique_ptr<T> std::unordered_set<std::unique_ptr<T>>, delete d: , erase() . splice(), , , , .

+5

- std::shared_ptr. , shared_ptr.

, , , -.

, std::string :

struct MyHasher
{
    size_t operator()(const std::shared_ptr<std::string>& element) const
    {
        return std::hash<std::string>()(*element);
    }
};

unordered_set :

std::unordered_set<std::string,MyHasher> the_set;

, .

0

All Articles