C++ Standard Library C++ STL Library

C++ unordered_multiset - insert() Function



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

Syntax

//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 unordered_multiset.

Time Complexity

  • Single element insertion: Average case - constant i.e, Θ(1). Worst case - linear i.e, Θ(n).
  • Multiple elements insertion: Average case - linear in number of elements inserted. Worst case - quadratic: number of elements inserted multiplied by (container size + 1).

Example:

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

#include <iostream>
#include <unordered_set>
using namespace std;
 
int main (){
  unordered_multiset<int> uMSet1 = {10, 20, 30};
  unordered_multiset<int> uMSet2 = {10, 20, 30};
  unordered_multiset<int>::iterator it;

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

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

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

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

  return 0;
}

The output of the above code will be:

uMSet1 contains: 10 55 20 30 
uMSet2 contains: 15 10 20 30 

Example:

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

#include <iostream>
#include <unordered_set>
#include <vector>
using namespace std;
 
int main (){
  unordered_multiset<int> uMSet = {10, 20, 30};
  vector<int> MyVec = {15, 30, 45, 60, 75};
  unordered_multiset<int>::iterator set_it;
  vector<int>::iterator vec_it;

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

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

  return 0;
}

The output of the above code will be:

uMSet contains: 15 45 10 20 30 30 

Example:

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

#include <iostream>
#include <unordered_set>
using namespace std;
 
int main (){
  unordered_multiset<int> uMSet = {10, 15};
  unordered_multiset<int>::iterator it;

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

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

  return 0;
}

The output of the above code will be:

uMSet contains: 14 13 12 11 10 15

❮ C++ <unordered_set> Library