Open In App

multiset insert() function in C++ STL

The multiset::insert() is a built-in function in C++ STL which insert elements in the multiset container or inserts the elements from a position to another position from one multiset to a different multiset. 

iterator multiset_name.insert(element)

Time Complexity: O(log n)

Since the elements are always in sorted order, the newly inserted elements should be added to its sorted order place and for finding the exact position of new element, a binary search is performed internally. Thus the binary search takes log n time to find the position to insert. Thus making the overall insertion time complexity as O(log n).

Parameters: The function accepts a mandatory parameter element which is to be inserted in the multiset container. 

Return Value: The function returns an iterator pointing to the inserted element in the multiset container. 

Below program illustrates the above function: 




// C++ program to demonstrate the
// multiset::insert(element) function
#include <bits/stdc++.h>
using namespace std;
int main()
{
 
    multiset<int> s;
 
    // Function to insert elements
    // in the set container
    s.insert(1);
    s.insert(4);
    s.insert(1);
    s.insert(5);
    s.insert(1);
 
    cout << "The elements in multiset are: ";
    for (auto it = s.begin(); it != s.end(); it++)
        cout << *it << " ";
 
    return 0;
}

Output: 
The elements in multiset are: 1 1 1 4 5

 

iterator multiset_name.insert(iterator position, element)

Parameters: The function accepts two parameters which are described below: 

Return Value: The function returns an iterator pointing to the inserted element in the multiset container.

Below program illustrates the above function:




// C++ program to demonstrate the
// multiset::insert(iterator, element) function
#include <bits/stdc++.h>
using namespace std;
int main()
{
 
    multiset<int> s;
 
    // Function to insert elements
    // in the set 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, 1);
    itr = s.insert(itr, 5);
 
    // Slow insertion as position is
    // not given correctly
    itr = s.insert(s.begin(), 3);
 
    cout << "The elements in multiset are: ";
    for (auto it = s.begin(); it != s.end(); it++)
        cout << *it << " ";
 
    return 0;
}

Output:

The elements in multiset are: 1 1 3 4 5
iterator multiset_name.insert(iterator position1, iterator position2)

Parameters: The function accepts two parameters position1 and position2 which specifies the range of elements. All the elements in the range [position1, last) are inserted in another set container. 

Return Value: The function returns a multiset which has all the elements in range [position1, last). 

Below program illustrates the above function: 




// C++ program to demonstrate the
// multiset::insert(iteratorposition1, iteratorposition2) function
#include <bits/stdc++.h>
using namespace std;
int main()
{
 
    multiset<int> s1;
 
    // Function to insert elements
    // in the set container
    s1.insert(1);
    s1.insert(4);
    s1.insert(1);
    s1.insert(5);
    s1.insert(1);
    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: 
The elements in multiset1 are: 1 1 1 3 4 5 
The elements in multiset2 are: 3 4 5

 

 

 


Article Tags :
C++