(这是关于std::move来自<algorithm>:https : //en.cppreference.com/w/cpp/algorithm/move)
AFAIK,有两种常见的方法可以将一个向量的内容移动到另一个向量的末尾。
第一的:
std::vector<T> dst, src;
...
dst.insert(dst.end(),
std::make_move_iterator(src.begin()),
std::make_move_iterator(src.end()));
Run Code Online (Sandbox Code Playgroud)
第二:
std::vector<T> dst, src;
...
std::move(src.begin(),
src.end(),
std::back_inserter(dst));
Run Code Online (Sandbox Code Playgroud)
这些都是进行连接的惯用方法吗?如果是这样,在语义方面有什么区别吗?
两者都应该完成工作,但存在细微差别。当您使用 时insert,向量有机会分配足够的内存以在一次分配中容纳所有元素。当您使用 时back_inserter,这相当于push_back可以进行多次分配和移动的多个s。