Javatpoint Logo
Javatpoint Logo

Unordered Set in C++

Overview

In C++, an unordered set is a container data structure that is used to hold elements without regard to their order. This article covers a wide range of topics, including what an unordered set is, how to create and initialize one in C++, and how it differs from a set in that language. In order to fully comprehend how an unordered set functions in C++ and how it may be utilized, we will also examine various examples and sample code.

What does C++'s Unordered set mean?

Introduction

In C++, an unordered set resembles a container data structure. It implies that we can store a variety of materials there. An unordered set is unique in that it only contains singular components. In C++, an unordered set will still only contain one instance of a repeated element if you introduce it. This container also has the unique feature of storing the components in any sequence. Because of this, the unordered set differs from the C++ set, which stores elements in ascending order.

Let's use an example to try to understand how an unordered set stores elements in C++. If we were to add the entries 1, 4, 9, 2, 1, and 10 to an unordered set in C++, the unordered set would appear as follows:

As you can see, element 1's duplicate was eliminated, and there is no particular order in which the elements should be placed.

Unordered sets are very helpful when you need to quickly and accurately determine whether a certain element occurs at least once in a collection of elements, count the number of distinct components, or both. In the next sections, we'll go into greater detail about unordered set C++'s complexity.

How do you create an unordered set?

Now that we are aware of the purposes for which an unordered set in C++ may be used, let's see how one might be created.

Firstly, the unordered set> header file contains the header library that enables us to use an unordered set in C++. We must include this at the beginning of our function to use an unordered set in C++.

Syntax:

In C++, the following syntax is used to declare an empty unordered set:

The data type of the elements that will be added into the unordered set is represented by data type in this case.

How do you initialise an unordered set?

The creation of an empty unordered set in C++ has been covered. However, if you wish to build an unordered set that already contains some elements, you must first import the unordered set> header file. Initializing an unordered set in C++ uses the following syntax:

These two approaches will both result in the same outcome. e1, e2, e3, e4,... are the items that are to be inserted into the unordered set when it is created, and data type indicates the data type of the elements that will be inserted in the unordered set.

Let's examine some C++ code for the unordered set function.

How is C++'s internal implementation of Unordered set done?

Unordered set C++ creation and initialization have been demonstrated, but how are the components stored within these containers internally? A hash table is used to implement the unordered set in C++. As a result, each element we add to our unordered set undergoes a hash operation to produce a hash key, which is then saved in the hash table. The unordered set in C++ stores elements randomly and in no particular order because the hash key is determined by the function and is generated through a randomized method.

It is another reason why the hash function's underlying complexity affects the complexity of the unordered collection. All operations on an unordered set in C++ typically require O(1) constant time, but in the worst case, they can take O(n) time, which is extremely uncommon.

Set vs. Unordered set

A set is an additional container element in C++ that is virtually identical to an unordered set. An unordered set differs from a set in that it stores its unique elements randomly and in no specific order, whereas a set arranges them in increasing order of their value.

Since a hash table is used to implement an unordered set in C++, which we previously covered, the elements are stored in random order. On the other hand, the C++ set implements a balanced tree, allowing it to hold elements in sorted order.

This variation in implementation affects both the time complexity and the various operations of both containers. While the set in C++ has an average time complexity of O(log(n)), the unordered set has an average time complexity of O(1) for all operations. But with n being the number of elements stored in them, both of these have a space complexity of O(n).

Unordered Set Methods in C++

Let's examine the many approaches that can be utilized with an unordered set in C++, together with their syntax and computational requirements.

Method Name Description Syntax Time complexity
insert() It is utilized to add a new element to the unordered set. set_name.insert(element); 0(1)
begin() It gives back an iterator that goes through the unordered set's first element. set_name.begin(); 0(1)
end() An iterator to the last element of the unordered set is returned. The location following the last element of the unordered set is indicated by this iterator. set_name.end(); 0(1)
count() In the unordered set, it is used to determine whether an element is present. If the element is present, it yields 1, otherwise it returns 0. set_name.count(element); 0(1)
size() It is used to return the unordered set's size or the total number of new, distinctive pieces that have been added to it. set_name.size(); 0(1)
find() The iterator pointing to a specific value in the unordered set is located and returned using this method. If the requested element cannot be located, an iterator to the end of the unordered set is returned. set_name.find(\*itr); 0(1)
empty() It is used to determine whether or not the unordered set is empty. If the unordered set is empty, it yields 1, otherwise it returns 0. set_name.empty() O(1)
erase() It is used to use iterators to remove a specific element or a group of elements from the unordered set. set_name.erase(*itr); OR set_name.erase(*itr_begin, *itr_end); 0(number of elements removed)
clear() The unordered set is totally emptied using it. set_name.clear(); 0(number of elements)

How to Iterate Over an Unordered Set's Elements in C++

In C++, we can use indexes to traverse through arrays, but there is no such thing as an index in an unordered set. On the other hand, iterators are pointers to the various items in the unordered set. We can traverse the unordered set and all of its items using these iterators.

As was demonstrated in the previous section, the begin() method returns an iterator pointing to the start of the unordered collection, where our loop can be initiated. The termination condition of our loop will be the iterator after the final element of the unordered set, which will be returned by the end() method.

Output

21 8 7 31 15 81 11

In the code above, we iterated until we reached the finishing iterator after initializing the iterator to the start of the unordered set. In C++, we can explore an unordered set's elements in the following manner.

Conclusion

In C++, an unordered set is a container data structure that is used to store distinct elements without regard to their order. The unordered set> header file contains the header library that enables us to utilize an unordered set in C++.

A hash table is used to implement the unordered set in C++. All operations on an unordered set in C++ typically take O(1) constant time, but they can occasionally take O(n) time.

Set differs from the unordered set in C++ in that it uses a balanced tree structure to implement and keeps unique elements in ascending order.

In C++, an unordered set can be accessed using a variety of functions, including insert(), begin(), end(), size(), empty(), clean(), erase(), count(), and find().







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA