![]() |
VOOZH | about |
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.
1 3 3 5
multiset<T> ms;
Where:
A multiset provides several built-in member functions to efficiently store, access, search, and remove elements while maintaining sorted order.
We can insert elements into a multiset by using insert() method. The multiset will automatically keep the elements sorted.
1 3 3 5
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().
1 3
Multiset provides fast search by value operation using the find() member function. This function returns iterator the element if found, otherwise returns end() iterator.
3
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.
1 3 3 5
The erase() method removes elements by value (all occurrences) or by iterator (specific element).
5
Note: Elements in a multiset cannot be updated or modified once inserted.