C++ Library - <multiset>



Introduction

A multiset is an associative container which contains a sorted collection of objects of type Key. Unlike set, multiple elements can have equivalent values, which means duplicates are allowed.

The value of the elements in a multiset cannot be modified in the container, i.e., the elements are always const. But they can be inserted or removed from the container.

The multiset containers are generally slower than unordered_multiset containers in accessing individual elements by their key, but they allow the direct iteration on subsets based on their order.

Characteristics of Multiset

Following are the key Characteristics of the multiset:

  • In multiset, the elements get automatically sorted. By default, it is sorted in ascending order.
  • The elements in multiset are always const.
  • Multiset is generally implemented as Red-Black tree(balanced binary search tree).
  • Multiset allows duplicate values.
Key difference from set: The main difference between a set and a multiset is that multiset accepts duplicate elements while set contains only unique elements.

Definition

Below is the definition of std::multiset from <set> header file

template < class Key,
    class Compare = std::less<Key>,
    class Allocator = std::allocator<Key>
> class multiset;

Parameters

  • Key: It represents the type of element contained. The key may be substituted by any other data type including user-defined type.

Member types

Following member types can be used as parameters or return type by member functions.

Member types Definition
key_type Key
value_type Key
reference value_type&
const_reference const value_type&
pointer std::allocator_traits<Allocator>::pointer
const_pointer std::allocator_traits<Allocator>::const_pointer
iterator BidirectionalIterator
const_iterator constant BidirectionalIterator
reverse_iterator std::reverse_iterator <iterator>
const_reverse_iterator std::reverse_iterator <const_iterator>
size_type Unsigned Integer Type (std::size_t)
difference_type Signed Integer Type (std::ptrdiff_t)
key_compare Compare
value_compare Compare
allocator_type Allocator

Functions from <multiset>

Below is list of all methods of multiset from <set> header.

Default Member Functions

Methods Description
multiset::multiset (default constructor) Constructs the multiset container.
Range constructor Constructs the multiset container with contents of the range.
Copy constructor Constructs the multiset container with the copy of other multiset.
Move constructor Constructs the multiset container with the contents of other multiset using move semantics.
Initializer-list constructor Constructs the multiset container with contents of the initializer list.
(destructor) Destructs the multiset container.
operator= Assigns values to the multiset container.

Iterators

Methods Description
multiset::begin It returns an iterator pointing to first element.
multiset::cbegin It returns the const iterator to beginning.
multiset::end It returns an iterator that points to element past the end.
multiset::cend Returns the const iterator to end.
multiset::rbegin Returns the reverse iterator to reverse beginning.
multiset::crbegin Return const reverse iterator to reverse beginning.
multiset::rend Returns the reverse iterator to reverse end.
multiset::crend Returns the const reverse iterator to reverse end.

Capacity

Methods Description
multiset::empty Returns whether the multiset container is empty.
multiset::size Returns the number of elements in the multiset container.
multiset::max_size Returns the maximum number of elements that the multiset container can hold.

Modifiers

Methods Description
multiset::clear Removes all elements from the multiset container.
multiset::insert Inserts new element in the multiset container.
multiset::emplace Inserts new element in the multiset.
multiset::emplace_hint Inserts new element in the multiset with a hint on the inserting position.
multiset::erase Removes either a single element or a range of elements from the multiset container.
multiset::swap Exchanges the content of the container by the content of another multiset container of the same type.

Lookup

Methods Description
multiset::count Returns the number of elements with matching value in the multiset container.
multiset::find Searches the multiset container for value and returns an iterator to it if found, else returns an iterator to multiset::end.
multiset::lower_bound Returns an iterator pointing to the first element in the multiset container which is not considered to go before value.
multiset::upper_bound Returns an iterator pointing to the first element in the multiset container which is considered to go after value.
multiset::equal_range Returns the bounds of a range that includes all the elements in the multiset container that are equivalent to value.

Observers

Methods Description
multiset::key_comp Returns a copy of the comparison object used by the multiset container.
multiset::value_comp Returns a copy of the comparison object used by the multiset container.

Allocator

Methods Description
multiset::get_allocator Returns a copy of the allocator object associated with the multiset container.

Example Usage

Here is an example to insert elements in the multiset and counting the occurrences of an element:

#include <iostream>
#include <set>

int main() {
    // Create a multiset
    std::multiset<int> ms;
    
    // Insert elements (duplicates allowed)
    ms.insert(10);
    ms.insert(20);
    ms.insert(10);  // Duplicate
    ms.insert(30);
    ms.insert(20);  // Duplicate
    
    // Display elements
    std::cout << "Multiset elements: ";
    for (auto it = ms.begin(); it != ms.end(); ++it) {
        std::cout << *it << " ";
    }
    std::cout << std::endl;
    
    // Count occurrences
    std::cout << "Count of 10: " << ms.count(10) << std::endl;
    
    return 0;
}

The output of the above code is as follows −

Multiset elements: 10 10 20 20 30
Count of 10: 2
Advertisements