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.

  1. Syntax:
    iterator multiset_name.insert(element)

    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:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // CPP 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;
    }

    chevron_right

    
    

    Output:



    The elements in multiset are: 1 1 1 4 5
    
  2. Syntax:
    iterator multiset_name.insert(iterator position, element)
    

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

    • element: It specifies the element to be inserted in the multiset container.
    • position: It does not specify the position where the insertion is to be done, it only points to a position from where the searching operation is to be started for insertion to make the process faster. The insertion is done according to the order which is followed by 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:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // CPP 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 isnertion 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;
    }

    chevron_right

    
    

    Output:

    The elements in multiset are: 1 1 3 4 5
    
  3. Syntax:
    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:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // CPP program to demonstrate the
    // multiset::insert(iterator1, iterator2) 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;
    }

    chevron_right

    
    

    Output:

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


My Personal Notes arrow_drop_up

The function of education is to teach one to think intensively and to think critically

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.