Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

adding elements of a vector to an unordered set

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

like image 680
jamesatha Avatar asked Oct 12 '12 01:10

jamesatha


People also ask

How do you add elements to an unordered set?

The unordered_set::insert() is a built-in function in C++ STL which is used to insert a new {element} in the unordered_set container. Each element is inserted only if it is not already present in the container (elements in an unordered_set have unique values).

How do you add vector elements to a set?

Get the vector to be converted. Create an empty set, to store the result. Iterate through the vector one by one, and insert each element into the set. Print the resultant set.

How do you store a vector in an unordered set?

An unordered set vector is an unordered associative container that is used to hold unique vectors together. Two vectors are considered the same if the corresponding elements of the vectors are equal. Unlike a set of vectors, vectors are not arranged in any particular order in an unordered set of vectors.

Can we add element in vector from front?

It works alot like a std::vector but you can add and remove items from both the front and the end. It does this by dividing the internal storage up into smaller blocks. You still have random-access iterators with good lookup speed.


1 Answers

If you're constructing the unordered_set then:

std::vector<int> v; std::unordered_set<int> s(v.begin(), v.end()); 
like image 69
mythagel Avatar answered Oct 08 '22 16:10

mythagel