Multiset is an associative container similar to the set, but it can store multiple elements with same value. It is sorted in increasing order by default, but it can be changed to any desired order.
Syntax
Multiset is defined as std::multiset class template inside <set> header file.
multiset<T, comp> ms;
where,
- T: Datatype of elements in the multiset.
- ms: Name assigned to the multiset.
- comp: It is a binary predicate function that tells multiset how to compare two elements in sorting. It is optional and if not provided, multiset is sorted in increasing order.
Declaration and Initialization
We can declare and initialize a multiset in multiple ways as shown in the below example:
C++ #include <bits/stdc++.h> using namespace std; int main() { // Creating an empty set of integers multiset<int> ms1; // Initialize with initializer list multiset<int> ms2 = {5, 3, 3, 1}; for (auto i : ms2) cout << i << " "; return 0; }
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.
Example:
C++ #include <bits/stdc++.h> using namespace std; int main() { multiset<int> ms; // Inserting elements ms.insert(5); ms.insert(3); ms.insert(3); ms.insert(1); for(auto i : ms) cout << i << " "; return 0; }
Accessing Elements
We can't use an index to get elements like in an array.
To reach a certain element, we start from begin() and move the iterator forward step by step or use helper functions like next() or advance().
But we can easily get the first element with begin() and the last element by moving one step back from end().
Example:
C++ #include <bits/stdc++.h> 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; }
Updating Elements
We cannot change the value of elements once they are stored in the set.
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.
Example:
C++ #include <bits/stdc++.h> using namespace std; int main() { multiset<int> ms = {5, 3, 3, 1}; // Finding 3 auto it = ms.find(3); if (it != ms.end()) cout << *it; else cout << "Not Found!"; return 0; }
Traversing
Multisets can be easily traversed using range-based for loop or using begin() and end() iterators. To traverse all the elements with same value(key), use the equal_range() function- it gives the start and end positions for the specific value.
Example:
C++ #include <bits/stdc++.h> using namespace std; int main() { multiset<int> ms = {5, 3, 3, 1}; // Traversing using range-based loop for(auto i : ms) cout << i << " "; return 0; }
Deleting Elements
To delete elements from a multiset, use the erase() method. This method can be used either by passing the value or an iterator. But if the value is passed, it deletes all the occurrences of that value.
Example:
C++ #include <bits/stdc++.h> using namespace std; int main() { multiset<int> ms = {5, 3, 3, 1}; // Delete first element ms.erase(ms.begin()); // Deleting all 3s ms.erase(3); for (auto x: ms) cout << x << " "; return 0; }
Multiset vs Set
Following is the primary differences between set and unordered_set in C++:
Feature | Set | Multiset |
---|
Duplicates | Not allowed (only unique elements) | Allowed (multiple copies allowed) |
---|
Order | Elements are sorted | Elements are sorted |
---|
Use case | When only unique values are needed | When duplicates are needed |
---|
Insertion | Inserts only if element not present. | Inserts elements regardless of duplicates |
---|
Explore
C++ Basics
Core Concepts
OOP in C++
Standard Template Library(STL)
Practice & Problems
My Profile