Multiset Insert Function - C++ Programming Tutorial
C++ Course / STL Set & Map / Multiset Insert Function

Multiset Insert Function

BLUF: Mastering Multiset Insert Function is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: Multiset Insert Function

C++ is renowned for its efficiency. Learn how Multiset Insert Function enables low-level control and high-performance computing in the tutorial below.

C++ multiset insert

The insert method in C++ Multiset is employed to add either a new element or a series of elements into the multiset data structure.

Syntax

Example

single element (1)     iterator insert (const value_type& val);   //until C++ 11

with hint (2)	iterator insert (iterator position, const value_type& val);   //until C++ 11

range (3)	  template <class InputIterator>
  		   void insert (InputIterator first, InputIterator last);        //until C++ 11

single element (1)  iterator insert (const value_type& val);
                                iterator insert (value_type&& val);                            //since C++ 11
            
with hint (2)	iterator insert (const_iterator position, const value_type& val);
                          iterator insert (const_iterator position, value_type&& val); //since C++ 11

range (3)	template <class InputIterator>
                          void insert (InputIterator first, InputIterator last);            //since C++ 11

initializer list (4)       void insert (initializer_list<value_type> il);              //since C++ 11

Parameter

val: Value to insert in the multiset.

Hint for the optimal placement of an element within the multiset.

first: Beginning of range to insert value.

last: End of range to insert value.

il: An initializer list.

Return value

The insert method provides an iterator that points to the recently added element within the multiset.

Complexity

If a solitary element is added, the complexity will scale logarithmically based on the size.

If a clue is provided and the specified location is ideal, the complexity will be constant on average.

Iterator validity

No changes.

Data Races

The container is modified.

Simultaneously accessing the current elements within a multiset is considered secure, but iterating over ranges in the container is not recommended.

Exception Safety

This function does not throw exception.

Example 1

Let's explore an uncomplicated illustration of adding elements to a multiset:

Example

#include <iostream>
#include <set>

using namespace std;

int main()
{
    multiset<int> s;
 
    s.insert(1);
    s.insert(4);
    s.insert(2);
    s.insert(3);
    s.insert(3);
 
    cout << "The elements in multiset are: ";
    for (auto it = s.begin(); it != s.end(); it++)
        cout << *it << " ";
 
    return 0;
}

Output:

Output

The elements in multiset are: 1 2 3 3 4

In the previously mentioned example, it merely adds the element with the specified key.

Example 2

Let's explore a basic illustration demonstrating the insertion of an element at a specific position:

Example

#include <iostream>
#include <set>

using namespace std;

int main()
{ 
    multiset<int> s;
 
    // Function to insert elements
    // in the multiset container
    auto itr = s.insert(s.begin(), 1);
 
    // the time taken to insertion
    // is very less as the correct
    // position for insertion is given
    itr = s.insert(itr, 4);
    itr = s.insert(itr, 2);
    itr = s.insert(itr, 4);
    itr = s.insert(itr, 3);
 
    cout << "The elements in multiset are: ";
    for (auto it = s.begin(); it != s.end(); it++)
        cout << *it << " ";
 
    return 0;
}

Output:

Output

The elements in multiset are: 1 2 3 4 4

In the example provided, elements are added at the specified location.

Example 3

Let's explore a straightforward illustration of transferring the elements from one multiset to another within a specified range:

Example

#include <iostream>
# include<iostream>
# include<set>

using namespace std;

int main()
{ 
    multiset<int> s1;
 
    // Function to insert elements
    // in the multiset container
    s1.insert(1);
    s1.insert(4);
    s1.insert(2);
    s1.insert(4);
    s1.insert(3);
 
    cout << "The elements in multiset1 are: ";
    for (auto it = s1.begin(); it != s1.end(); it++)
        cout << *it << " ";
 
    multiset<int> s2;
 
    // Function to insert one multiset to another
    // all elements from where 3 is to end is
    // inserted to multiset2
    s2.insert(s1.find(3), s1.end());
 
    cout << "\nThe elements in multiset2 are: ";
    for (auto it = s2.begin(); it != s2.end(); it++)
        cout << *it << " ";
 
    return 0;
}

Output:

Output

The elements in multiset1 are: 1 2 3 4 4 
The elements in multiset2 are: 3 4 4

Example 4

Let's explore a straightforward example of inserting elements from the initializer list:

Example

#include <iostream>
#include <set>

using namespace std;

int main(void) {
   multiset<string> m = {"Java", "C++", "SQL"};
   
  // Insert the elements from an initializer_list 
   m.insert({"C++", "Oracle"});

   cout << "Multiset contains following elements" << endl;

   for (auto it = m.begin(); it != m.end(); ++it)
      cout << *it<< endl;

   return 0;
}

Output:

Output

Multiset contains following elements
C++
C++
Java
Oracle
SQL

In the aforementioned example, components are added from an initializer list.

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience