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.
This solution actually requires more characters, but it does express the intent more directly:
std::unordered_set<T> ht(std::make_move_iterator(v.begin()),
std::make_move_iterator(v.end()));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With