I have a vector<T>
that I would like to initialize unordered_set<T>
with. vector<T>
will never be used again afterwards.
How I've been doing it is the following
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()));
I am wondering if there's a more direct way to do this with unordered_set
constructor? Its move constructor doesn't take in a vector.
std::back_inserter(ht)
actually compile? I thoughtback_inserter
needs the object to havepush_back
. – Infrequentstd::move(v.begin(), v.end(), std::inserter(ht, ht.end()));
? – Hanfurdvector
into anunordered_set
, you can only move its contents. – Hortensiahorter