Different Ways to Initialize an unordered_set in C++
Last Updated :
17 Apr, 2023
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++
- Initialization using the default constructor
- Initialization using an initializer list
- Initialization using an array
- Initialization using a vector
- Initialization from another set using the copy constructor
- 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++
#include <iostream>
#include <unordered_set>
using namespace std;
int main()
{
unordered_set<string>New_set;
New_set.insert( "Ground" );
New_set.insert( "Grass" );
New_set.insert( "Floor" );
New_set.insert( "Table" );
New_set.insert( "Wood" );
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++
#include <iostream>
#include <unordered_set>
using namespace std;
int main()
{
unordered_set<string>New_set({ "Ground" ,
"Grass" ,
"Floor" ,
"Table" ,
"Wood" });
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++
#include <bits/stdc++.h>
using namespace std;
int main()
{
string old_arr[] = { "Ground" ,
"Grass" ,
"Floor" ,
"Cement" ,
"Table" };
int n = ( sizeof (old_arr) /
sizeof (old_arr[0]));
unordered_set<string>New_set(old_arr,
old_arr + n);
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++
#include <bits/stdc++.h>
using namespace std;
int main()
{
vector<string>old_arr = { "Ground" ,
"Grass" ,
"Floor" ,
"Cement" ,
"Table" };
unordered_set<string>New_set(old_arr.begin(),
old_arr.end());
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++
#include <iostream>
#include <unordered_set>
using namespace std;
int main()
{
unordered_set<string>old_set;
old_set.insert( "Ground" );
old_set.insert( "Grass" );
old_set.insert( "Floor" );
old_set.insert( "Table" );
old_set.insert( "Wood" );
unordered_set<string>New_set(old_set);
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++
#include <iostream>
#include <unordered_set>
using namespace std;
int main()
{
unordered_set<string>old_set;
old_set.insert( "Ground" );
old_set.insert( "Grass" );
old_set.insert( "Floor" );
old_set.insert( "Table" );
old_set.insert( "Wood" );
unordered_set<string>New_set(begin(old_set),
end(old_set));
for ( auto x: New_set)
{
cout << x <<endl;
}
return 0;
}
|
Output
Grass
Ground
Floor
Table
Wood
Similar Reads
Unordered Sets in C++ STL
In C++, unordered_set is an unordered associative container that stores unique elements. Unlike set, it stores its elements using hashing. This provides average constant-time O(1) search, insert, and delete operations but the elements are not sorted in any particular order. Example: [GFGTABS] C++ #i
7 min read
Different Ways to Initialize an unordered_set in C++
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++ Initializati
6 min read
unordered set of Vectors in C++ with Examples
What is an unordered set? In C++, an unordered set is an unordered container that can hold a number of unique elements. Unlike a set, elements in an unordered set are not arranged in any particular order. Internally, an unordered set is implemented using a hash table where keys are hashed into indic
6 min read
unordered set of Pairs in C++ with Examples
What is pair? Utility header in C++ provides us pair container. A pair consists of two data elements or objects. The first element is referenced as âfirstâ and the second element as âsecondâ and the order is fixed (first, second).Pair is used to combine together two values that may be different in t
5 min read
How to create an unordered_set of user defined class or struct in C++?
The unordered_set internally implements a hash table to store elements. By default we can store only predefined type as int, string, float etc. If we want to store the element of user defined type as structure then compiler will show an error because before storing elements into unordered_set compil
3 min read
set vs unordered_set in C++ STL
Differences : | set | unordered_set --------------------------------------------------------- Ordering | increasing order | no ordering | (by default) | Implementation | Self balancing BST | Hash Table | like Red-Black Tree | search time | log(n) | O(1) -> Average | | O(n) -> Worst Case Insert
4 min read