我有一个std::set<std::unique_ptr<T>>
我想把它移到std::vector<std::unique_ptr<T>>
#include <set>
#include <vector>
#include <memory>
class C {};
int main()
{
std::set<std::unique_ptr<const C>> s;
std::vector<std::unique_ptr<const C>> v;
std::move(s.begin(), s.end(), std::back_inserter(v));
}
在VS2017上出现以下错误:
错误 C2280: 'std::unique_ptr>::unique_ptr(const std::unique_ptr<_ty>> &)':尝试引用已删除的函数
我们不能将迭代器从 a 移动到非常量变量吗std::set
?什么是解决这个问题的可行方案?
为了从集合中提取仅移动元素,唯一的可能性是使用extract http://en.cppreference.com/w/cpp/container/set/extract方法,在C++17中添加:
while (!s.empty())
v.emplace_back(std::move(s.extract(s.begin()).value()));
如果您不能使用 C++17,则允许修改集合的元素(例如使用mutable
) only if您确保它在强加的排序中保留相同的位置 - 也就是说,只要与集合中的所有其他成员相比,它在您的比较器下具有相同的结果。您可以通过提供一个比较器来实现这一点,该比较器在非空之前对空的唯一指针进行排序(请注意,标准不保证这一点)并在修改元素后立即删除它:
template<class T> struct MutableWrapper { mutable T value; };
template<class T> struct MutableWrapperCompare {
bool operator()(MutableWrapper<T> const& lhs, MutableWrapper<T> const& rhs) {
return lhs.value && rhs.value ? lhs.value < rhs.value : rhs.value;
}
};
int main()
{
std::set<MutableWrapper<std::unique_ptr<const C>>, MutableWrapperCompare<std::unique_ptr<const C>>> s;
std::vector<std::unique_ptr<const C>> v;
while (!s.empty())
{
v.emplace_back(std::move(s.begin()->value));
s.erase(s.begin());
}
}
然而,这是相当丑陋和危险的;你最好使用boost::container::set
from Boost.Container http://www.boost.org/doc/libs/1_64_0/doc/html/container.html,其中具有 C++17 提取方法(从 1.62.0 开始 https://github.com/boostorg/container/commit/4298d1984bd8f43f637e42b510f827309a9a13c5; it 没有证件 https://github.com/boostorg/container/pull/52,但这只是一个疏忽,注意相应的extract
方法记录为map
and multimap
).
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)