Set Container in C++ STL:
Sets are a type of associative containers in which each element has to be unique, because the value of the element identifies it. The value of the element cannot be modified once it is added to the set, although it is possible to remove the previous value and add the modified value again to the set.
Sets are very useful to store information the form of a list, although, we have arrays for that purpose, but the performance of arrays is less than that of set. Also, the sets are very useful in solving the problems related to sorting, as the elements inserted in a set are in sorted order by default.
Some basic functions associated with Set:
- begin() – Returns an iterator to the first element in the set.
- end() – Returns an iterator to the theoretical element that follows last element in the set.
- size() – Returns the number of elements in the set.
- insert(key) – Inserts a new element key in the Set.
- find(key) – Returns an iterator pointing to the element key in the set if it is present.
- empty() – Returns whether the set is empty.
In the code above, we have created a set and an array.
The elements in the array are being stored randomly, such as {40, 20, 60, 30, 50, 50, 10}.
We would now insert these elements of the array to the set we just instantiated.
We can simply insert the elements to the set using the insert function provided to us by set in STL.
After that, we have used the cout statement in the for loop and iterator to print the elements of the set.
After that, we are checking for the element 50, whether it is present or not and displaying the result using the cout statement.
The Output of the above code will be:
The elements in the set are:
10 20 30 40 50 60
50 is present
You might have noticed that elements are displayed in the sorted order and duplicate element such as 50 is also removed to keep all elements unique.
Unordered_set in C++ STL:
The unordered_set is implemented using the Hash Table so it is not possible to maintain the index of the elements. All elements are stored randomly, so there’s no specific order of the elements in an unordered_set. Due to it’s randomness, performance of unordered_set is very high and takes O(1) time on an average.
Unordered_set can contain any type of object as a key, but we need to specify our own compare function for the unordered_set to compare the values while computing them.
Set is implemented as a balanced tree structure that is why it is possible to maintain order between the elements (by specific tree traversal). The time complexity of set operations is O(Log(n)) while for unordered_set, it is O(1).
We can use other operation on the unordered_set such as ‘s.size()’ or ‘s.find(itr)’. The find operation takes O(1) on an average.
In this code, we have implemented an unordered_set and inserted the elements similar to the previous example of set.
The Output of the code is:
The elements in the unordered_set are:
10 50 30 60 40 20
50 is present
In the output, you might have noticed that elements are not sorted in any order, and that’s why, it’s called unordered_set. Though, the duplicate element such as 50 is removed here also, as all sets, ordered and unordered are created in such a way that they does not allow duplicates.