Multiset is an associative container similar to a set, but it can store multiple elements with the same value. It is sorted in increasing order by default, but it can be changed to any desired order using a custom comparator.
- Allows duplicate elements, unlike a set.
- Supports standard set operations like insert(), erase(), count(), and find().
- It also internally seems to be using a Self-Balancing Binary Search Tree (like a set), handling duplicates by making a rule to either insert in the left or right subtree to ensure O(Log n) time complexity of operations.
Example:
#include <iostream>
#include <set>
using namespace std;
int main()
{
// Creating an empty multiset of integers
multiset<int> ms1;
// Initialize with initializer list
multiset<int> ms2 = {5, 3, 3, 1};
// Print elements (automatically sorted)
for (auto i : ms2)
cout << i << " ";
return 0;
}
Output
1 3 3 5
Syntax
multiset<T> ms;
Where:
- T -> data type of elements
- ms -> name of the multiset
Basic Operations
Here are the basic operations that can be performed on a multiset:
Inserting Elements
- We can insert elements into a multiset by using insert() method. The multiset will automatically keep the elements sorted.
#include <iostream>
#include <set>
using namespace std;
int main()
{
// Creating a multiset of integers
multiset<int> ms;
// Inserting elements (duplicates allowed)
ms.insert(5);
ms.insert(3);
ms.insert(3);
ms.insert(1);
// Print elements (automatically sorted)
for (auto i : ms)
cout << i << " ";
return 0;
}
Output
1 3 3 5
Accessing Elements
Elements cannot be accessed by index; use an iterator from begin() and advance it. The first element is *begin(), and the last is one step back from end().
#include <iostream>
#include <iterator>
#include <set>
using namespace std;
int main()
{
multiset<int> ms = {5, 3, 3, 1};
// Access first element
auto it1 = ms.begin();
cout << *it1 << " ";
// Access third element
auto it2 = next(it1, 2);
cout << *it2;
return 0;
}
Output
1 3
Finding Elements
Multiset provides fast search by value operation using the find() member function. This function returns iterator the element if found, otherwise returns end() iterator.
#include <iostream>
#include <set>
using namespace std;
int main()
{
multiset<int> ms = {5, 3, 3, 1};
// Finding an element (3)
auto it = ms.find(3);
if (it != ms.end())
cout << *it;
else
cout << "Not Found!";
return 0;
}
Output
3
Traversing
Use a range-based for loop or begin()/end() iterators to traverse a multiset. And It use equal_range() to traverse all elements with the same value.
#include <iostream>
#include <set>
using namespace std;
int main()
{
multiset<int> ms = {5, 3, 3, 1};
// Traversing using range-based for loop
for (auto i : ms)
cout << i << " ";
return 0;
}
Output
1 3 3 5
Deleting Elements
The erase() method removes elements by value (all occurrences) or by iterator (specific element).
#include <iostream>
#include <set>
using namespace std;
int main()
{
multiset<int> ms = {5, 3, 3, 1};
// Delete first element
ms.erase(ms.begin());
// Delete all occurrences of 3
ms.erase(3);
// Print remaining elements
for (auto x : ms)
cout << x << " ";
return 0;
}
Output
5
Note: Elements in a multiset cannot be updated or modified once inserted.