Insert elements to MultiSet in C++

This tutorial will discuss how to insert element in multiset in C++?.

To insert elements into a multiset, the container provides a member function named ‘insert()’. This function accepts a value as an argument and inserts the element into the multiset. Additionally, it returns an iterator pointing to the newly inserted element. Let’s see a practical example,

#include <iostream>
#include <set>

int main()
{
    // Create an empty multiset
    std::multiset<std::string> words;

    // Insert elements in the multiset
    words.insert("this");
    words.insert("why");
    words.insert("at");
    words.insert("how");
}

In the given example, we created an empty multiset and then added four string values to it. The content of multiset will be,

at, how, this, why,

A multiset can accommodate duplicate elements. Therefore, if we attempt to insert a duplicate element that already exists in the multiset, it will still be added as a new entry, as shown in the example below.

// Insert duplicate elements in the Set
words.insert("at");
words.insert("at");
words.insert("why");
words.insert("why");

Although the string values ‘at’ and ‘why’ were already present in the multiset, we inserted them again. Consequently, all the elements were added without any issues. This behavior can be confirmed by displaying the contents of the multiset. The content of multiset will be,

at, at, at, how, this, why, why, why,

The ‘insert()’ function also returns an iterator pointing to the newly added element. In the subsequent example, we inserted a string ‘last’ into the multiset of strings and displayed the added element using the iterator returned by the ‘insert()’ function.

#include <iostream>
#include <set>

int main()
{
    // Create an empty multiset
    std::multiset<std::string> words;

    auto it = words.insert("last");

    if(it != words.end())
    {
        std::cout << "Element Inserted: "<< *it << std::endl;
    }

}

Output:



    Element Inserted: last
    

    This demonstrates how we can insert elements into a multiset.

    Let’s see the complete example,

    #include <iostream>
    #include <string>
    #include <vector>
    #include <set>
    
    int main()
    {
        // Create an empty multiset
        std::multiset<std::string> words;
    
        // Insert elements in the multiset
        words.insert("this");
        words.insert("why");
        words.insert("at");
        words.insert("how");
    
        // Iterate over set and print elements
        for(const auto& elem : words)
        {
            std::cout<< elem<< ", ";
        }
        std::cout<<std::endl;
    
        // Insert duplicate elements in the Set
        words.insert("at");
        words.insert("at");
        words.insert("why");
        words.insert("why");
    
        // Iterate over set and print elements
        for(const auto& elem : words)
        {
            std::cout<< elem<< ", ";
        }
        std::cout<<std::endl;
    
        auto it = words.insert("last");
    
        if(it != words.end())
        {
            std::cout << "Element Inserted: "<< *it << std::endl;
        }
    
        // Iterate over set and print elements
        for(const auto& elem : words)
        {
            std::cout<< elem<< ", ";
        }
        std::cout<<std::endl;
    
    
        std::vector<int> vecOfnumbers {11, 22, 33, 33, 33, 44, 33, 55};
        std::set<int> numSet;
    
        // Insert multiple elements in Set in one line
        numSet.insert(vecOfnumbers.begin(), vecOfnumbers.end());
    
        // Iterate over set and print elements
        for(const auto& elem : numSet)
        {
            std::cout<< elem<< ", ";
        }
        std::cout<<std::endl;
    
    
        std::set<int> numSetObj;
    
        // Insert multiple elements in Set in one line
        numSetObj.insert({11, 22, 22, 33, 22, 44});
    
        // Iterate over set and print elements
        for(const auto& elem : numSetObj)
        {
            std::cout<< elem<< ", ";
        }
        std::cout<<std::endl;
    }
    

    Output

    at, how, this, why, 
    at, at, at, how, this, why, why, why, 
    Element Inserted: last
    at, at, at, how, last, this, why, why, why, 
    11, 22, 33, 44, 55, 
    11, 22, 33, 44,
    

    Summary

    Today, we learned how to insert element in multiset in C++?.

    Scroll to Top