C++ Standard Library C++ STL Library

C++ multiset - multiset() Function



The C++ multiset::multiset function is used to construct a multiset object, initializing its contents depending on the version of constructor used:

Syntax

//default version - construct an empty 
//container with no elements
explicit multiset (const key_compare& comp = key_compare(),
                   const allocator_type& alloc = allocator_type());

//range version - Constructs a container with 
//elements as the range [first,last)
template <class InputIterator>
  multiset (InputIterator first, InputIterator last,
            const key_compare& comp = key_compare(),
            const allocator_type& alloc = allocator_type());

//copy version - copies all elements
//of x into the container	
multiset (const multiset& x);
//default version - construct an empty 
//container with no elements  
explicit multiset (const key_compare& comp = key_compare(),
                   const allocator_type& alloc = allocator_type());
explicit multiset (const allocator_type& alloc);

//range version - Constructs a container with 
//elements as the range [first,last)
template <class InputIterator>
  multiset (InputIterator first, InputIterator last,
            const key_compare& comp = key_compare(),
            const allocator_type& = allocator_type());

//copy version - copies all elements
//of x into the container	
multiset (const multiset& x);
multiset (const multiset& x, const allocator_type& alloc);

//move version - moves elements of x
//into the container	
multiset (multiset&& x);
multiset (multiset&& x, const allocator_type& alloc);

//initializer list version - copies all 
//elements of il into the container	
multiset (initializer_list<value_type> il,
          const key_compare& comp = key_compare(),
          const allocator_type& alloc = allocator_type());
//default version - construct an empty 
//container with no elements
mutiset();
explicit multiset (const key_compare& comp,
                   const allocator_type& alloc = allocator_type());
explicit multiset (const allocator_type& alloc);

//range version - Constructs a container with 
//elements as the range [first,last)
template <class InputIterator>
  multiset (InputIterator first, InputIterator last,
            const key_compare& comp = key_compare(),
            const allocator_type& = allocator_type());
template <class InputIterator>
  multiset (InputIterator first, InputIterator last,
            const allocator_type& = allocator_type());

//copy version - copies all elements
//of x into the container	
multiset (const multiset& x);
multiset (const multiset& x, const allocator_type& alloc);

//move version - moves elements of x
//into the container	
multiset (multiset&& x);
multiset (multiset&& x, const allocator_type& alloc);

//initializer list version - copies all 
//elements of il into the container		
multiset (initializer_list<value_type> il,
          const key_compare& comp = key_compare(),
          const allocator_type& alloc = allocator_type());
multiset (initializer_list<value_type> il,
          const allocator_type& alloc = allocator_type());

Parameters

alloc Specify the Allocator object. The container keeps and uses an internal copy of this allocator.
comp A binary predicate that takes two elements of multiset as arguments and returns a bool. Elements are sorted by using this function.
first Specify initial position of the input iterator of the range. The range used is [first,last).
last Specify final position of the input iterator of the range. The range used is [first,last).
x Specify a multiset object of same type.
il Specify an initializer_list object.

Return Value

Constructor never returns value.

Time Complexity

Constant i.e, Θ(1), for default version and move version.
For all the other cases, Linear i.e, Θ(n).

Example: using default version

In the example below, the multiset::multiset function is used to construct a multiset object.

#include <iostream>
#include <set>
using namespace std;
 
int main (){
  //default version - construct an empty multiset
  multiset<int> MSet;
  multiset<int>::iterator it;

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

  //insert elements in MSet
  MSet.insert(10);
  MSet.insert(20);

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

  return 0;
}

The output of the above code will be:

MSet contains: 
MSet contains: 10 20 

Example: using range and copy version

A multiset can also be constructed using range or copy version. Consider the following example:

#include <iostream>
#include <set>
using namespace std;
 
int main (){
  multiset<int> MSet1{100, 200, 300};
  multiset<int>::iterator it;

  //range version - construct MSet2 using range
  multiset<int> MSet2(MSet1.begin(), MSet1.end());

  //copy version - construct MSet3 from MSet1
  multiset<int> MSet3(MSet1); 

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

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

  return 0;
}

The output of the above code will be:

MSet2 contains: 100 200 300
MSet3 contains: 100 200 300 

Example: using move version

Using the move version of multiset, the content of one multiset can be moved to another multiset. Consider the following example:

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

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

  //moving all content of MSet1 into MSet2
  multiset<int> MSet2(move(MSet1));

  cout<<"\nMSet1 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 40 50 
MSet1 contains: 
MSet2 contains: 10 20 30 40 50 

Example: using initializer list version

The initializer list can also be used to assign values into a multiset container. Consider the example below:

#include <iostream>
#include <set>
using namespace std;
 
int main (){
  //creating initializer list
  initializer_list<int> ilist = {15, 30, 45, 60, 75};

  //initializer list version - copies all 
  //elements of ilist into the container	
  multiset<int> MSet(ilist);
  multiset<int>::iterator it;

  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: 15 30 45 60 75 

❮ C++ <set> Library