Lets Implement Sets & Multisets:

Devanshi
Nerd For Tech
Published in
2 min readSep 6, 2020
Image source ccplusplus

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:

inserting values in set

Deleting Elements from the set:

We can delete elements from the set using the erase function either by passing values or bypassing the iterator.

Deleting elements using erase

Printing elements of the set:

We can simply print the values with the help of iterators:

Printing elements from set

→ 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:

  1. Its a set like a container that can store multiple elements having the same value.
  2. The elements are sorted according to the internal comparison object however by default it is sorted in ascending order
  3. Just like set here also the values cannot be updated
  4. It is an associative container as elements are referred by their value.
  5. 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:

Output of above code

End of article

Hope it would help get an overview of the set and a multiset

Thank you

Happy Coding

--

--