Open In App

Different Ways to Initialize an unordered_set in C++

Last Updated : 17 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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

Time Complexity : O(N)

Auxiliary Space : O(N)

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

Time Complexity : O(N)where N is the number of elements in the unordered_set. This is because accessing elements in an unordered_set has an average time complexity of O(1) due to its hash-based implementation.

Auxiliary Space : O(N), as the unordered_set stores the elements in the set and the size of the unordered_set is proportional to the number of elements stored in it. Therefore, the space used by the unordered_set is directly

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

Time Complexity : O(N), where ‘n’ is the number of elements in the array ‘old_arr’. This is because the loop that iterates through the array to populate the unordered_set has a linear time complexity, as it iterates through each element once.

Auxiliary Space : O(M),where ‘m’ is the number of unique elements in the array ‘old_arr’. This is because the unordered_set stores the elements in a hash table, which requires additional space for the hash table itself, as well as the elements and their associated hashes. The actual space usage may vary depending on the implementation of the unordered_set and the hash function used. In the worst case scenario, the space complexity could be O(n), if all the elements collide and end up in the same bucket of the hash table. However, on average, the space complexity of unordered_set is considered to be O(m).

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


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads