std ::将项目移出STL容器?

时间:2016-11-04 01:41:12

标签: c++ c++11 std move-semantics

我正在尝试std::move std::set中的一个元素,然后将其从集合中删除:

std::set<some_type> the_set;
some_type item;   

for (auto iter = the_set.begin(); iter != the_set.end(); iter++)
{
    auto miter = std::make_move_iterator(iter);
    item = std::move(*miter);
    the_set.erase(iter);
    break;
}

编译器不喜欢它(MSVC 2015):

error C2280: 'some_type &some_type::operator =(const some_type &)': attempting to reference a deleted function

似乎它正在尝试使用复制构造函数,这是我不想要的。如何使用移动构造函数? (我在绝望中尝试了move_iterator,不确定这是否是正确的解决方案。)

1 个答案:

答案 0 :(得分:3)

Thanks for registering! Here's your activation link: {{ request.scheme }}://{{ request.get_host }}{% url 'registration_activate' activation_key %} 迭代器实际上是std::set迭代器(在C ++ 11及更高版本中)。这样,你就不可能改变它们并打破const的订购。

你无法从set移动。所以你不能离开const&&

C ++ 17允许你这样做,但是via a different API (PDF)。基本上,您必须extract the node itself from the set,然后移动节点中的对象,然后销毁节点(让它从堆栈中掉落)。我相信代码看起来像这样:

set
相关问题