如果我有兩個向量包含std :: unique_ptr <>,有沒有辦法將向量b添加到向量a的末尾,從而刪除向量b?如何合併兩個包含std :: unique_ptr的向量?
例如:
std::unique_ptr<std::vector<int>> a(&someintvector);
std::unique_ptr<std::vector<int>> b(&someotherintvector);
我怎麼會去運動向量b,以向量的結束?
如果我有兩個向量包含std :: unique_ptr <>,有沒有辦法將向量b添加到向量a的末尾,從而刪除向量b?如何合併兩個包含std :: unique_ptr的向量?
例如:
std::unique_ptr<std::vector<int>> a(&someintvector);
std::unique_ptr<std::vector<int>> b(&someotherintvector);
我怎麼會去運動向量b,以向量的結束?
你移動的b
內容爲a
:
std::move(std::begin(*b), std::end(*b), std::back_inserter(*a));
如果矢量也包含一個類,這是否工作?因爲我得到以下錯誤:沒有重載函數「std :: begin」的實例匹配參數列表參數類型是:(std :: unique_ptr
@qwesr請參閱編輯。 – 0x499602D2 2014-10-11 18:28:10
@Loopunroller感謝您的編輯。 – 0x499602D2 2014-10-11 18:28:44
另一種方式的元素移動到a
:
a->insert(a->end(), std::make_move_iterator(b->begin()), std::make_move_iterator(b->end()));
也許[這](http://stackoverflow.com/q/ 9778238/893693)可以幫助你,但我不確定'unique_ptr'在你的代碼中的作用。 – inf 2014-10-11 18:24:18