Skip to content
Related Articles

Related Articles

Different Ways to Initialize an unordered_set in C++

View Discussion
Improve Article
Save Article
Like Article
  • Last Updated : 20 Apr, 2022

An unordered_set is an associated container available in the C++ Standard Template Library(STL) that is used for unique elements without any specific ordering, it internally uses the working principle of a hashtable to store elements.

Different ways to Initialize an unordered_set in C++

  1. Initialization using the default constructor
  2. Initialization using an initializer list
  3. Initialization using an array
  4. Initialization using a vector
  5. Initialization from another set using the copy constructor
  6. Initialization from another iterable data structure using range constructor

Let’s discuss each of these topics in detail.

1. Initialization Using the Default Constructor

One standard way to initialize an unordered_set is to initialize using the default constructor, this will generate an empty unordered_set. We can further add elements into it using inbuilt unordered_set.insert() method. 

Syntax:

unordered_set<string>New_set;
New_set.insert(element1)

Here, insert() method can be further used to insert elements to the unordered_set

Below is the C++ program to implement the above approach:

C++




// C++ program to implement
// the above approach
#include <iostream>
#include <unordered_set>
using namespace std;
 
// Driver code
int main()
{   
    // Initialize unordered_set
    // using default constructor
    unordered_set<string>New_set;
   
    // unordered_set.insert() method to
    // insert elements to the unordered_set
    New_set.insert("Ground");
    New_set.insert("Grass");
    New_set.insert("Floor");
    New_set.insert("Table");
    New_set.insert("Wood");
     
    // Traverse through the unordered_set
    for(auto x: New_set)
    {
       cout << x << endl;
    }
    return 0;
}

Output

Wood
Table
Floor
Ground
Grass

2. Initialization Using an Initializer List

Another way of initialization is to pass a predefined list of elements (initializer_list) as an argument to the default constructor of the unordered_set.

Syntax:

unordered_set<string>New_set({element1, element2, element3, element4}); 

Below is the C++ program to implement the above approach:

C++




// C++ program to implement
// the above approach
#include <iostream>
#include <unordered_set>
using namespace std;
 
// Driver code
int main()
{   
    // Initialize unordered_set passing
    // initializer list as an argument
    // to the default constructor
    unordered_set<string>New_set({"Ground",
                                  "Grass",
                                  "Floor",
                                  "Table" ,
                                  "Wood"});
     
    // Traverse through the unordered_set
    for(auto x: New_set)
    {
       cout << x << endl;
    }  
    return 0;
}

Output

Wood
Table
Floor
Grass
Ground

3. Initialization Using an Array

As unordered_set stores unique elements, one can store the elements using an array of the same data type.

Syntax:

unordered_set<string>New_set(old_arr, old_arr + n); 

Here, old_arr is the array of strings from which contents will be copied into the New_set.

Below is the C++ program to implement the above approach:

C++




// C++ program to implement
// the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Driver code
int main()
{
    // Initialize an array of pair
    // of strings
    string old_arr[] = {"Ground",
                        "Grass" ,
                        "Floor",
                        "Cement",
                        "Table"};
    int n = (sizeof(old_arr) /
             sizeof(old_arr[0]));
   
    // Adding these elements stored
    // in the array   
    unordered_set<string>New_set(old_arr,
                                 old_arr + n);
   
    // Traverse through the unordered_map
    for(auto x: New_set)
    {
       cout << x << endl;
    }
    return 0;
}

Output

Table
Cement
Floor
Grass
Ground

4. Initialization using a Vector

One can store the elements to the unordered_set using a vector of the same data type.

Syntax:

unordered_set<string>New_set(old_vector.begin(), old_vector.end()); 

Here, old_vector is the vector of strings from which contents will be copied into the New_set.

Below is the C++ program to implement the above approach:

C++




// C++ program to implement
// the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Driver code
int main()
{
    // Initialize an array of pair
    // of strings
    vector<string>old_arr = {"Ground",
                             "Grass",
                             "Floor",
                             "Cement",
                             "Table"};
   
    // Adding these elements stored
    // in the vector   
    unordered_set<string>New_set(old_arr.begin(),
                                 old_arr.end());
   
    // Traverse through the unordered_map
    for(auto x: New_set)
    {
       cout << x << endl;
    }
    return 0;
}

Output

Table
Cement
Floor
Grass
Ground

5. Initialization From Another Set Using the Copy Constructor

One way to initialize a unordered_set is to copy contents from another set one after another by using the copy constructor.

Syntax:

unordered_set<string>New_set(old_set);

Here, old_set is the set from which contents will be copied into the new_set.

Below is the C++ program to implement the above approach:

C++




// C++ program to implement
// the above approach
#include <iostream>
#include <unordered_set>
using namespace std;
 
// Driver code
int main()
{
    // Initialize an unordered_set
    // using default constructor
    unordered_set<string>old_set;
   
    // unordered_set.insert() method
    // to insert elements to the
     // unordered_set
    old_set.insert("Ground");
    old_set.insert("Grass");
    old_set.insert("Floor");
    old_set.insert("Table");
    old_set.insert("Wood");
   
   
    // Create a new_set where contents
    // of the previous set will be copied
    // using copy constructor   
    unordered_set<string>New_set(old_set);
   
    // Traverse through the unordered_map
    for(auto x: New_set)
    {
       cout << x <<endl;
    }   
    return 0;
}

Output

Wood
Table
Floor
Ground
Grass

6. Initialization From Another Iterable Data Structure Using Range Constructor

Another way to initialize an unordered_set is to use a range constructor to copy elements from an iterable data structure (unordered_set in this example) to the newly initialized unordered_set. 

Syntax:

unordered_set<string>New_set(begin(old_set), end(old_set));

Here, old_set is the set from which contents will be copied into the New_set.

Below is the C++ program to implement the above approach:

C++




// C++ program to implement
// the above approach
#include <iostream>
#include <unordered_set>
using namespace std;
 
// Driver code
int main()
{
    // Initialize an unordered_set using
    // default constructor
    unordered_set<string>old_set;
   
    // unordered_set.insert() method to
    // insert elements to the unordered_set
    old_set.insert("Ground");
    old_set.insert("Grass");
    old_set.insert("Floor");
    old_set.insert("Table");
    old_set.insert("Wood");
   
   
    // Create a new_set where contents of
    // the previous set will be copied using
    // range constructor   
    unordered_set<string>New_set(begin(old_set),
                                 end(old_set));
   
    // Traverse through the unordered_map
    for(auto x: New_set)
    {
       cout << x <<endl;
    }
    return 0;
}

Output

Grass
Ground
Floor
Table
Wood


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!