The std::multiset is an associative container provided by the C++ Standard Library. It is similar to std::set, but there’s a major distinction. In a set, only unique elements can be stored, whereas in a multiset, duplicate elements are allowed. Both set and multiset store their elements in sorted order, not in the order they were inserted. By default, this sorting is in ascending order, but we can change it to descending order using a custom comparator, just as we can with set. Furthermore, we can create a multiset of user-defined data types as well.

To create a multiset object, we need to include the set header file:

#include <set>

How to create a MultiSet in C++?

When creating a multiset object, we need to provide the template parameter as the data type of the elements that will be stored in the multiset. We can then initialize the multiset with default values. For instance:

#include <iostream>
#include <set>

int main()
{
    // Create a multiset of integers and initialize
    std::multiset<int> mSetObj = {3, 5, 5, 3, 6, 7, 6, 3, 2, 5, 9, 8};

    // Print the multiset.
    for (int val : mSetObj)
    {
        std::cout << val << " ";
    }
    std::cout << std::endl;

    return 0;
}

Output:

2 3 3 3 5 5 5 6 6 7 8 9 

In the above example, we initialize our multiset with 12 values. It contains certain duplicate values too. All the values are stored in the multiset because a multiset can contain duplicate elements. Furthermore, all these values are stored in sorted order, which is ascending by default.

We can iterate over the contents of a multiset using either iterators or a range-based for loop, similar to other containers like set or vector.

How to find elements in a MultiSet in C++ ?

std::multiset provides a find() function which accepts a value as an argument and returns an iterator pointing to the first occurrence of that particular value. Since a multiset can contain duplicate values, there might be multiple occurrences of the given value. However, the find() function will only return the iterator pointing to the first occurrence.

#include <iostream>
#include <set>

int main()
{
    // Create a multiset of integers and initialize
    std::multiset<int> mSetObj = {3, 5, 5, 3, 6, 7, 6, 3, 2, 5, 9, 8};

    int target = 7;
    // find element in multiset
    auto iter = mSetObj.find(target);

    if (iter != mSetObj.end())
    {
        std::cout << "Element " << target << " found. It points to the first occurrence." << std::endl;
    }
    else
    {
        std::cout << "Element " << target << " not found." << std::endl;
    }

    return 0;
}

Output:

Element 7 found. It points to the first occurrence.

It’s possible that you might be interested in all occurrences of a particular value, including duplicate entries. For this purpose, multiset offers a function called equal_range. It accepts a value as an argument and returns a pair of iterators. The first iterator in this pair points to the first occurrence of the given value, and the second iterator points to the element just after the last occurrence of the value. If the specified value does not exist in the multiset, then both iterators will point to the same location, which will be the element just beyond the last value in the set.

#include <iostream>
#include <set>

int main()
{
    // Create a multiset of integers and initialize
    std::multiset<int> mSetObj = {3, 5, 5, 3, 6, 7, 6, 3, 2, 5, 9, 8};

    // Using equal_range function to find all matching values
    auto range = mSetObj.equal_range(5);

    std::cout << "Range of values equal to 5:" << std::endl;
    for (auto itr = range.first; itr != range.second; ++itr)
    {
        std::cout << *itr << " ";
    }
    std::cout << std::endl;


    return 0;
}

Output:

Range of values equal to 5:
5 5 5 

How to insert elements in a MultiSet in C++?

std::multiset also provides a insert function. It accepts a value as an argument and inserts that value into the multiset. Since multiset maintains elements in sorted order, when we insert an element, the container automatically positions it to preserve this order.

Unlike std::set, the insert function in multiset never fails. This is because a multiset allows duplicate elements. When you use the insert function, it takes a value as an argument, adds it to the container, and returns an iterator pointing to the newly inserted value in the multiset.

Let’s see an example,

#include <iostream>
#include <set>

int main()
{
    // Create a multiset of integers and initialize
    std::multiset<int> mSetObj = {3, 5, 5, 3, 6, 7, 6, 3, 2, 5, 9, 8};

    // Inserting elements into the multiset.
    mSetObj.insert(0);            // Insert single element
    mSetObj.insert({15, 5, 3, 2, 7}); // Insert multiple elements

    std::cout << "After inserting elements:" << std::endl;
    for (int val : mSetObj)
    {
        std::cout << val << " ";
    }
    std::cout << std::endl;


    return 0;
}

Output:

After inserting elements:
0 2 2 3 3 3 3 5 5 5 5 6 6 7 7 8 9 15 

Summary

std::multiset is an associative container similar to std::set. However, in a set, only unique elements are allowed, whereas in a multiset, duplicate elements can be stored. Both set and multiset store their elements in sorted order, which is in ascending order by default.

Ritika Ohri

Hi, I am Ritika Ohri, founder of this blog. I craft comprehensive programming tutorials and also manage a YouTube channel. You can also connect with me on Linkedin.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.