multiset insert() function in C++ STL - GeeksforGeeks

17 Nov, 2020
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. 
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: 
C++
// 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;}
The elements in multiset are: 1 1 1 4 5





 
Syntax: 
iterator multiset_name.insert(iterator position, element)


Parameters: The function accepts two parameters 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:
C++
// 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

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: 
C++
// 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;}
The elements in multiset1 are: 1 1 1 3 4 5 
The elements in multiset2 are: 3 4 5





 
 
 
arorakashish0911
CPP-Functions
cpp-multiset
STL
C++
STL
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Inheritance in C++
C++ Classes and Objects
Bitwise Operators in C/C++
Operator Overloading in C++
Constructors in C++
Socket Programming in C/C++
Virtual Function in C++
Multidimensional Arrays in C / C++
Templates in C++ with Examples
Copy Constructor in C++