Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

C++ Program to Implement Multiset in STL



A multiset is an associative container that stores data in a sorted order in such a way that it allows duplicate values in the set. In this article, we will learn how to use a multiset from the Standard Template Library (STL) in C++.

What is Multiset?

Multiset is a sorted associative container that allows storing multiple elements with the same value. The values are automatically arranged in sorted order. This makes it useful in scenarios where you need to keep count of repeated values while maintaining order.

For example, we can store the scores of students even if there are repeated scores:

multiset<int> scores;
scores.insert(90);
scores.insert(85);
scores.insert(90);

// Display all scores
for (int score : scores) {
    cout << score << " ";
}

//Output will be displayed as:
85 90 90

Using multiset Class in STL

The <multiset> class is part of the C++ standard STL library. It stores values in sorted order and allows duplicate values. Below are some functions used in multiset class:

  • insert(): Adds a new element into the multiset, allowing duplicates.
  • erase(): Removes elements by value or iterator.
  • find(): Returns an iterator pointing to the first element equal to the given value.
  • count(): Returns the number of elements with the specified value.
  • equal_range(): Returns a pair of iterators to the range of equal elements.
  • clear(): Removes all elements from the multiset.

Steps to Implement Multiset in C++ STL

Following are steps/algorithm to implement a multiset using C++ STL:

  • Create a multiset using std::multiset.
  • Insert values using insert() function.
  • Access duplicate elements using equal_range().
  • Remove elements using erase().
  • Iterate and display multiset elements using a loop.

C++ Program to Implement Multiset using STL

The below code is implemention of the above algorithm in C++ language.

#include <iostream>
#include <set>
using namespace std;

int main() {
    multiset<int> numbers;

    // Insert elements including duplicates
    numbers.insert(5);
    numbers.insert(3);
    numbers.insert(5);
    numbers.insert(2);

    // Display multiset elements
    cout << "Multiset Elements:" << endl;
    for (int num : numbers) {
        cout << num << " ";
    }
    cout << endl;

    // Display all entries with value 5
    auto range = numbers.equal_range(5);
    cout << "Occurrences of 5:" << endl;
    for (auto it = range.first; it != range.second; ++it) {
        cout << *it << " ";
    }
    cout << endl;

    return 0;
}

The output of above code will be

Multiset Elements:
2 3 5 5
Occurrences of 5:
5 5

Time and Space Complexity

Time Complexity:

  • Insertion, Deletion, and Search: O(log n) per operation since multiset uses balanced BST.
  • Iteration: O(n) for visiting all elements.

Space Complexity: O(n) where n is the number of elements in the multiset.

Updated on: 2025-05-07T18:29:43+05:30

220 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements