C++ Standard Library C++ STL Library

C++ unordered_multiset - unordered_multiset() Function



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

Syntax

//default version - construct an empty 
//container with no elements
explicit unordered_multiset ( size_type n = /* see below */,
                              const hasher& hf = hasher(),
                              const key_equal& eql = key_equal(),
                              const allocator_type& alloc = allocator_type() );
explicit unordered_multiset ( const allocator_type& alloc );

//range version - Constructs a container with 
//elements as the range [first,last)
template <class InputIterator>
         unordered_multiset ( InputIterator first, InputIterator last,
                              size_type n = /* see below */,
                              const hasher& hf = hasher(),
                              const key_equal& eql = key_equal(),
                              const allocator_type& alloc = allocator_type() );

//copy version - copies all elements
//of ust into the container	
unordered_multiset ( const unordered_multiset& ums );
unordered_multiset ( const unordered_multiset& ums, const allocator_type& alloc );

//move version - moves elements of ust
//into the container	
unordered_multiset ( unordered_multiset&& ums );
unordered_multiset ( unordered_multiset&& ums, const allocator_type& alloc );

//initializer list version - copies all 
//elements of il into the container	
unordered_multiset (initializer_list<value_type> il,
                    size_type n = /* see below */,
                    const hasher& hf = hasher(),
                    const key_equal& eql = key_equal(),
                    const allocator_type& alloc = allocator_type() );
//default version - construct an empty 
//container with no elements
unordered_multiset();
explicit unordered_multiset ( size_type n,
                              const hasher& hf = hasher(),
                              const key_equal& eql = key_equal(),
                              const allocator_type& alloc = allocator_type() );
explicit unordered_multiset ( const allocator_type& alloc );
         unordered_multiset ( size_type n, const allocator_type& alloc );
         unordered_multiset ( size_type n, const hasher& hf, const allocator_type& alloc );

//range version - Constructs a container with 
//elements as the range [first,last)
template <class InputIterator>
  unordered_multiset ( InputIterator first, InputIterator last,
                       size_type n = /* see below */,
                       const hasher& hf = hasher(),
                       const key_equal& eql = key_equal(),
                       const allocator_type& alloc = allocator_type() );
template <class InputIterator>
  unordered_multiset ( InputIterator first, InputIterator last,
                       size_type n, const allocator_type& alloc );
template <class InputIterator>
  unordered_multiset ( InputIterator first, InputIterator last,
                       size_type n, const hasher& hf, const allocator_type& alloc );

//copy version - copies all elements
//of ust into the container	
unordered_multiset ( const unordered_multiset& ums );
unordered_multiset ( const unordered_multiset& ums, const allocator_type& alloc );

//move version - moves elements of ust
//into the container	
unordered_multiset ( unordered_multiset&& ums );
unordered_multiset ( unordered_multiset&& ums, const allocator_type& alloc );

//initializer list version - copies all 
//elements of il into the container		
unordered_multiset (initializer_list<value_type> il,
                     size_type n = /* see below */,
                     const hasher& hf = hasher(),
                     const key_equal& eql = key_equal(),
                     const allocator_type& alloc = allocator_type() );
unordered_multiset ( initializer_list<value_type> il, size_type n,
                     const allocator_type& alloc );
unordered_multiset ( initializer_list<value_type> il, size_type n, const hasher& hf,
                     const allocator_type& alloc );

Parameters

alloc Specify the Allocator object. The container keeps and uses an internal copy of this allocator.
n It contains information about minimum number of initial buckets.
hf It is a hasher function object.
eql The comparison function object, that returns true if the two container object keys passed as arguments are equal.
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).
ums Specify an unordered_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, Average case: Linear i.e, Θ(n), Worst case: Quadratic i.e, Θ(n2).

Example: using default version

In the example below, the unordered_multiset::unordered_multiset function is used to construct an unordered_multiset object.

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

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

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

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

  return 0;
}

The output of the above code will be:

uMSet contains: 
uMSet contains: 20 10 

Example: using range and copy version

An unordered_multiset can also be constructed using range or copy version. Consider the following example:

#include <iostream>
#include <unordered_set>
using namespace std;
 
int main (){
  unordered_multiset<int> uMSet1{100, 200, 300};
  unordered_multiset<int>::iterator it;

  //range version - construct uMSet2 using range
  unordered_multiset<int> uMSet2(uMSet1.begin(), uMSet1.end());

  //copy version - construct uMSet3 from uMSet1
  unordered_multiset<int> uMSet3(uMSet1); 

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

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

  return 0;
}

The output of the above code will be:

uMSet2 contains: 100 200 300 
uMSet3 contains: 300 200 100 

Example: using move version

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

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

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

  //moving all content of uMSet1 into uMSet2
  unordered_multiset<int> uMSet2(move(uMSet1));

  cout<<"\nuMSet1 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: 50 40 30 20 10 
uMSet1 contains: 
uMSet2 contains: 50 40 30 20 10 

Example: using initializer list version

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

#include <iostream>
#include <unordered_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	
  unordered_multiset<int> uMSet(ilist);
  unordered_multiset<int>::iterator it;

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

❮ C++ <unordered_set> Library