Is it possible to move an item out of a std::set?

前端 未结 2 710
忘了有多久
忘了有多久 2020-12-17 18:35

If I have an object that only allows move-only semantics - is it possible to move items from a set? I can\'t seem to find a way to do this.

2条回答
  •  爱一瞬间的悲伤
    2020-12-17 19:09

    C++17 added a function std::set<>::extract that allows moving an object out of a set:

    std::set s;
    s.emplace(arg0, arg1, arg2); // only way to insert such move-only objects, since C++11
    auto internal_node = s.extract(s.begin()); // internal_node no longer part of set, we can do with it what we want
    MoveOnlyType m = std::move(internal_node.value()); // finally get the actual object out
    

提交回复
热议问题