![](/img/trans.png)
[英]Efficiently moving contents of std::unordered_set to std::vector
[英]Moving a vector into an unordered_set
我有一個vector<T>
,我想用它來初始化unordered_set<T>
。 vector<T>
將不再使用。
我的做法如下
std::vector<T> v{ /* some large amount of data, typically strings */ };
std::unordered_set<T> ht;
std::move(v.begin(), v.end(), std::inserter(ht, ht.end()));
我想知道是否有更直接的方法來使用unordered_set
構造函數來做到這一點? 它的移動構造函數不接受向量。
這個解決方案實際上需要更多的字符,但它確實更直接地表達了意圖:
std::unordered_set<T> ht(std::make_move_iterator(v.begin()),
std::make_move_iterator(v.end()));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.