我想将一个 vector 附加到另一个 vector 的末尾。据我所知,函数 std::move()
是此任务的“首选函数”。为什么 Microsoft Visual C++ Express 中的 std::move()
会崩溃,而手工制作的循环却按预期工作?
我正在使用 Microsoft Visual C++ 2015 Update 3。不幸的是,我无法使用其他编译器对此进行测试。
// The setup code for the two vectors:
vector<unique_ptr<channel>> channels, added_channels;
// ... here is some code that adds some elements to both vectors
据我所知,以下两段代码应该以相同的方式工作。他们应该将 added_channels
的元素移动到 channels
的末尾。
这是第一个崩溃的变体:
std::move(added_channels.begin(), added_channels.end(), channels.end());
这是第二个有效的版本:
for(auto & ref : added_channels)
{
channels.push_back(std::move(ref));
}
最佳答案
std::move 移动到特定位置。
如果你想将它插入到 vector 的后面,你应该使用 std::back_inserter
std::move(added_channels.begin(), added_channels.end(), std::back_inserter(channels));
关于c++ - Visual C++ 为什么 std::move 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48381058/