use*_*573 7 c++ stdvector visual-c++ c++11
我想在另一个向量的末尾追加一个向量.据我所知,该功能std::move()是此任务的"选择功能".为什么std::move()在手工制作的循环按预期工作时,Microsoft Visual C++ Express会崩溃?
我使用的是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
Run Code Online (Sandbox Code Playgroud)
根据我的知识,以下两段代码应该以相同的方式工作.他们应该把元素added_channels移到最后channels.
这是第一个崩溃的变种:
std::move(added_channels.begin(), added_channels.end(), channels.end());
Run Code Online (Sandbox Code Playgroud)
这是第二个有效的版本:
for(auto & ref : added_channels)
{
channels.push_back(std::move(ref));
}
Run Code Online (Sandbox Code Playgroud)
Yoc*_*mer 13
std :: move移动到特定位置.
如果要将其插入向量的背面,则应使用std :: back_inserter
std::move(added_channels.begin(), added_channels.end(), std::back_inserter(channels));
Run Code Online (Sandbox Code Playgroud)
This is actually one of the few cases where a move_iterator is actually useful:
channels.insert(channels.end(),
std::make_move_iterator(added_channels.begin()),
std::make_move_iterator(added_channels.end()));
Run Code Online (Sandbox Code Playgroud)
For insertion into a vector, range insert is preferable to element-by-element push_back (or the equivalent back_inserter) because it avoids unnecessary reallocations and correctly grows the storage exponentially. The latter is easy to mess up: indiscriminate uses of reserve can easily cause quadratic behavior.
| 归档时间: |
|
| 查看次数: |
485 次 |
| 最近记录: |