Lets Implement Sets & Multisets:
What is set and why do we need them:
Set is a container used to store unique elements. By default, it is ordered and is used when we want to remove duplicate elements. We cant update any element of a set the only way is to remove that element and insert the other one.
1)Implementation of set stl :
Inserting elements in set:
We can insert the values directly in the set using the insert function:
Deleting Elements from the set:
We can delete elements from the set using the erase function either by passing values or bypassing the iterator.
Printing elements of the set:
We can simply print the values with the help of iterators:
→ Lower bound will return an iterator to position of element greater than or equal to the given element.
→The Upper bound will return an iterator to the position of elements greater than the given element.
Properties Of a Multiset:
- Its a set like a container that can store multiple elements having the same value.
- The elements are sorted according to the internal comparison object however by default it is sorted in ascending order
- Just like set here also the values cannot be updated
- It is an associative container as elements are referred by their value.
- The underlying data structure used in a Multiset is a binary search tree.
Implementation of some operations on a multiset:
The output of the above code:
End of article
Hope it would help get an overview of the set and a multiset
Thank you
Happy Coding