What is the difference between Set vs Hashset in C++?
Last Updated :
23 May, 2024
In C++, both set and HashSet(also called unordered_set) are used to store elements but they have different properties and use cases. In this article, we will learn the key differences between a set and a HashSet in C++.
Set in C++
In C++, a std::set is a container that stores only the unique elements in a sorted fashion. We can add duplicate elements in any random order but when we retrieve the elements we will get the unique elements in a sorted manner only. Following are the important points regarding set in C++:
- Set are implemented using self-balancing Binary Search Trees, therefore they are ordered.
- Elements in a set are always sorted in increasing order.
- The time complexity for searching in a set is O(log(n)).
Syntax to Declare Set
set <dataType> setName;
Example
The below example demonstrates the use of a set in C++.
C++
// C++ program to demonstrate the use of set container
#include <iostream>
#include <set>
using namespace std;
int main()
{
// creating a set of integer type
set<int> st;
// Inserting values in random order and with duplicates
// in a set
st.insert(10);
st.insert(5);
st.insert(10);
st.insert(15);
// printing the element in a set
for (auto it : st) {
cout << it << ' ';
}
return 0;
}
Time Complexity: O(log n), where n is the number of elements in the set,
Auxilliary Space: O(n)
HashSet in C++
In C++ , a HashSet or std::unordered_set both are same. It is a container which is also used to store elements but unlike set it does not store elements in a sorted order. Following are some important points about HashSet:
- HashSet is not part of the C++ standard. It’s an extension and its equivalent is unordered_set in the C++ standard library.
- HashSet is implemented using hash tables.
- The key element, is hashed into an index of the hash table and gets stored at that particular index.
- The time complexity to search an element in a HashSet is O(1), which is faster than a set.
- When we iterate through a HashSet, the order of elements will be random.
Syntax to Declare HashSet
unordered_set<dataType> unordered_set_name;
Example
The below example demonstrates the use of a HashSet in C++.
C++
// C++ program to demonstrate the use of HashSet container
#include <iostream>
#include <unordered_set>
using namespace std;
int main()
{
// creating a HashSet of integer type
unordered_set<int> ust;
// Inserting values in random order and with duplicates
// in a HashSet
ust.insert(10);
ust.insert(5);
ust.insert(10);
ust.insert(15);
// printing the element in a set
for (auto it : ust) {
cout << it << ' ';
}
return 0;
}
Time Complexity: O(1), but can degrade to O(n) in the worst case due to hash collisions.
Auxilliary Space: O(n)
Difference Between Set and HashSet in C++
To understand the difference between Set and HashSet in C++, we need to know what each container does.
Feature
| Set
| HashSet
|
---|
Order of elements
| Elements are sorted in increasing order.
| Elements are not sorted in any specific order.
|
---|
Implementation
| Self balancing Binary Search Trees.
| Hash Table
|
---|
Search Time Complexity
| O(log(n))
| O(1)
|
---|
Standard
| It is a part of C++ standard
| It is not part of C++ standard, it’s an extension.
|
---|
Header File
| Sets are included in the #include<set>
| unordered_set is included using the #include<unordered_set>
|
---|
Conclusion
In conclusion, to choose between Set and HashSet depends on our specific needs. If we need the elements to be sorted, we can use a set. If we need faster access and searching and don’t care about the order of elements, we can use a HashSet instead. The basic understanding of the key differences between these two containers is important for implementing them.
Similar Reads
Difference between std::set vs std::vector in C++ STL Vectors: Vectors are containers similar to dynamic arrays, with the ability to resize when a new element is inserted or deleted from it. It is a template of Standard Template Library or STL, which provides more flexibility to the program. Elements of vectors are placed in contiguous storage and are
2 min read
Difference between pair in Multiset and Multimap in C++ STL Pairs in C++: The pair container is a simple container defined in <utility> header consisting 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 which
5 min read
Different Ways to Initialize an Set in C++ Initializing a set means assigning some initial values to the elements of the set container. In this article, we will learn different methods to initialize an std::set in C++.Table of ContentUsing Initializer ListOne by One InitializationFrom Another std::setFrom Another STL Container or ArrayUsing
3 min read
How to Find the Size of a Set in Bytes in C++? In C++, sets are associative containers that only store unique elements. The elements inside the set are sorted in a specific order. In this article, we will learn how we can find the size of a set in bytes in C++. Example Input: S = {1,2,3,4}Output: Size of the set in bytes is : 16Find the Size of
2 min read
How to Delete an Element from the Set in C++? In C++, the set container stores unique elements in the sorted order and the deletion operation should not disrupt this sorted order.C++ provides a built-in function set erase(), which can be used to easily delete the given element by passing its value to the function. Let's take a look at the simpl
1 min read
How to Create a Set of Sets in C++? In C++, sets are STL containers that store unique elements of the same type in a sorted manner. Sets of sets, also known as nested sets, are collections in which each element of the outer set contains another set as its element. In this article, we will learn how to create a set of sets in C++. Set
2 min read