adding elements of a vector to an unordered set

jamesatha picture jamesatha · Oct 12, 2012 · Viewed 27.7k times · Source

Is there an easy way to add all the elements of a vector to an unordered_set? They are of the same type. Right now, I am using a for loop and was wondering if there is a better way to do it

Answer

mythagel picture mythagel · Oct 12, 2012

If you're constructing the unordered_set then:

std::vector<int> v;
std::unordered_set<int> s(v.begin(), v.end());