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
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).
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.
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.
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.
If you're constructing the unordered_set then:
std::vector<int> v; std::unordered_set<int> s(v.begin(), 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