In my code I have a std::unordered_set
and I need to move the data into a std::vector
. I'm using the std::unordered_set
while getting the data to ensure only unique values are stored prior to converting to a std::vector
. My question is how do I move the contents to the std::vector
the most efficiently? I don't need the std::unordered_set
after the data is moved. I currently have the following:
std::copy(set.begin(), set.end(), std::back_inserter(vector));
std::vector::reserve
would help (if you're not doing that already). – Compeerstd::vector<double>
– Hewittdouble
, copy is as efficient as move, but you should reserve as LogicStuff recommends. Separately, if in your case it takes time for the data to arrive (e.g. over the network, from a huge file doing async I/O etc) and you don't actually need the vector sorted, you could check whether it's the first time you've seen a value as you attempt insertion to the set, and if so also insert to the vector so it's ready to use as soon as the last input's received (though this works best if you know the number of elements to reserve beforehand). – Paracasein