multiset insert() function in C++ STL
Last Updated :
15 Jun, 2022
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++
#include <bits/stdc++.h>
using namespace std;
int main()
{
multiset< int > s;
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:
- 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++
#include <bits/stdc++.h>
using namespace std;
int main()
{
multiset< int > s;
auto itr = s.insert(s.begin(), 1);
itr = s.insert(itr, 4);
itr = s.insert(itr, 1);
itr = s.insert(itr, 5);
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++
#include <bits/stdc++.h>
using namespace std;
int main()
{
multiset< int > s1;
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;
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
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...