Facebook Page Twitter Page LinkedIn Page
× C++ Standard Library C++ STL Library


The C++ multiset::insert function is used to insert new elements in the container. This results into increasing the multiset size by the number of elements inserted.

Syntax

//single element version  
iterator insert (const value_type& val);

//single element with hint version  
iterator insert (iterator position, const value_type& val);

//range version 
template <class InputIterator>
  void insert (InputIterator first, InputIterator last);
//single element version 
iterator insert (const value_type& val);
iterator insert (value_type&& val);

//single element with hint version   
iterator insert (const_iterator position, const value_type& val);
iterator insert (const_iterator position, value_type&& val);

//range version 
template <class InputIterator>
  void insert (InputIterator first, InputIterator last);

//initializer list version 
void insert (initializer_list<value_type> il);

Parameters

position Specify the hint for the position where the element can be inserted.
val Specify the value to be copied (or moved) to the inserted elements.
first Specify the starting position of InputIterator. Copies of the elements in the range [first,last) are inserted in the container.
last Specify the last position of InputIterator. Copies of the elements in the range [first,last) are inserted in the container.
il Specify the initializer_list object.

Return Value

Returns an iterator pointing to the newly inserted element in the multiset.

Time Complexity

  • Logarithmic i.e, Θ(log(n)) if a single element is inserted, but constant i.e, Θ(1) if position provided is optimal.
  • Multiple elements insertion: Average case - linear in number of elements inserted. Worst case - Linearithmic: number of elements inserted multiplied by log of (container size + number of elements inserted).

Example:

In the below example, the multiset::insert function is used to insert elements in the given multiset.

#include <iostream>
#include <set>
using namespace std;
 
int main (){
  multiset<int> MSet1 = {10, 20, 30};
  multiset<int> MSet2 = {10, 20, 30};
  multiset<int>::iterator it;

  //single element version 
  MSet1.insert(55);

  //single element with hint version 
  it = MSet2.begin();
  MSet2.insert(++it, 15);

  cout<<"MSet1 contains: ";
  for(it = MSet1.begin(); it != MSet1.end(); ++it)
    cout<<*it<<" ";

  cout<<"\nMSet2 contains: ";
  for(it = MSet2.begin(); it != MSet2.end(); ++it)
    cout<<*it<<" ";

  return 0;
}

The output of the above code will be:

MSet1 contains: 10 20 30 55 
MSet2 contains: 10 15 20 30 

Example:

A range of elements can also be inserted into a multiset. Consider the below example.

#include <iostream>
#include <set>
#include <vector>
using namespace std;
 
int main (){
  multiset<int> MSet = {10, 20, 30};
  vector<int> MyVec = {15, 30, 45, 60, 75};
  multiset<int>::iterator multiset_it;
  vector<int>::iterator vec_it;

  //range version - insert a range of 
  //elements of MyVec into MSet
  vec_it = MyVec.begin();
  MSet.insert(vec_it, vec_it + 3);

  cout<<"MSet contains: ";
  for(multiset_it = MSet.begin(); multiset_it != MSet.end(); ++multiset_it)
    cout<<*multiset_it<<" ";

  return 0;
}

The output of the above code will be:

MSet contains: 10 15 20 30 30 45 

Example:

Similarly, the initializer list version can be used to insert elements in the given multiset.

#include <iostream>
#include <set>
using namespace std;
 
int main (){
  multiset<int> MSet = {10, 15};
  multiset<int>::iterator it;

  //initializer list version 
  initializer_list<int> ilist = {11, 12, 13, 14};
  MSet.insert(ilist); 

  cout<<"MSet contains: ";
  for(it = MSet.begin(); it != MSet.end(); ++it)
    cout<<*it<<" ";

  return 0;
}

The output of the above code will be:

MSet contains: 10 11 12 13 14 15 

❮ C++ - Multiset