【发布时间】:2020-08-17 03:26:30
【问题描述】:
假设我有一个std::vector<T> from 和std::vector<T> to,其中T 是不可复制但可移动的类型,to 可能为空,也可能不为空。我希望from 中的所有元素都附加在to 之后。
如果我将 std::vector<T>::insert(const_iterator pos, InputIt first, InputIt last) 重载 (4) 与 pos = to.end() 一起使用,它将尝试复制所有对象。
现在,如果to.empty() 我可以只使用std::move(from),否则我可以先from.reserve(from.size()+to.size()),然后手动to.emplace_back(std::move(from[i]))from 的每个元素,最后是from.clear()。
有没有使用std 便利函数或包装器的直接方法?
【问题讨论】:
标签: c++ c++17 stdvector move-semantics