In this article we will discuss the different ways to initialize an unordered_set.

Creating an empty unordered_set and then inserting elements in it is easy. But its not the practical scenario always. Many times we need to initialize an unordered_set while creation. For that it provides different overloaded constructors and we will use some of them to initialize an unordered_set from an another datastructure i.e.

Initializing an unordered_set from an array

unordered_set provides a constructor that accepts a range of iterators and try to insert all elements in while iterating from start to end of range i.e.

Above set will contain only unique elements from array and store them in arbitrary order.

Initializing an unordered_set by an initialzer list

unordered_set provides a constructor that accepts an initialzer_list as an argument and initialize the set with them i.e.

Initializing an unordered_set with a vector

For this we will use the same constructor of unordered_set that accepts a range of iterators and initialize set with all elements in range i.e.

Complete Working example is as follows,

Output:

 

Click Here to Subscribe for more Articles / Tutorials like this.