[英]visual c++ why does std::move crash
我想在另一個向量的末尾追加一個向量。 據我所知,函數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移動到特定位置。
如果要將其插入向量的背面,則應使用std :: back_inserter
std::move(added_channels.begin(), added_channels.end(), std::back_inserter(channels));
這實際上是move_iterator
實際上有用的少數情況之一:
channels.insert(channels.end(),
std::make_move_iterator(added_channels.begin()),
std::make_move_iterator(added_channels.end()));
為了插入vector
,范圍insert
優於逐元素push_back
(或等效的back_inserter
),因為它避免了不必要的重新分配並正確地以指數方式增加存儲。 后者容易搞亂:不加選擇地使用reserve
很容易導致二次行為。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.