Multiset Constructor

There are following five uses of multiset constructor :

  • default (empty) constructor : This is used to construct an empty multiset container with zero elements.
  • range constructor : This is used to construct a container with the contents of range [first, last).
  • copy constructor : This is used to construct a multiset with a copy of the elements of existing container.
  • move constructor : This is used to construct the container with the elements of other with the use of move semantics.
  • initializer list constructor : This is used to construct the multiset with the content of the initializer list.
  • Syntax

Default constructor

Example

explicit multiset (const key_compare& comp = key_compare(),
              const allocator_type& alloc = allocator_type());	            //until C++ 11

explicit multiset (const key_compare& comp = key_compare(),
              const allocator_type& alloc = allocator_type());
explicit multiset (const allocator_type& alloc);			//until C++ 14	

mutiset();
explicit multiset (const key_compare& comp,
                   const allocator_type& alloc = allocator_type());		//since C++ 14
explicit multiset (const allocator_type& alloc);

range constructor

Example

template <class InputIterator>
  multiset (InputIterator first, InputIterator last,
       const key_compare& comp = key_compare(),
       const allocator_type& alloc = allocator_type());		//until C++ 11

template <class InputIterator>
  multiset (InputIterator first, InputIterator last,
       const key_compare& comp = key_compare(),
       const allocator_type& = allocator_type());			//until C++ 14	

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());		//since C++ 14

copy constructor

Example

multiset (const multiset& x);						//until C++ 11
	
multiset (const multiset& x);
multiset (const multiset& x, const allocator_type& alloc);			//since C++ 11

move constructor

Example

multiset (multiset&& x);
multiset (multiset&& x, const allocator_type& alloc);			//since C++ 11

initializer list constructor

Example

multiset (initializer_list<value_type> il,
     const key_compare& comp = key_compare(),
     const allocator_type& alloc = allocator_type());		//until C++ 14

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());		//since C++ 14

Parameter

comp : A comparison function object which takes two key arguments and returns true if first argument goes before the second argument otherwise, it returns false. By default, it uses less<key_type> predicate.

alloc : An allocator object use for all memory allocations of this container.

first : Input iterator to the first position in a range.

last : Input iterator to the last position in a range.

x : Another multiset object of the same type.

il : An initializer list object from which the elements are to be copied.

Return value

Constructor never returns any value.

Complexity

For empty constructors and move constructors, complexity will be constant.

For all other cases, complexity will be linear in the distance between the iterators if the elements are already sorted.

Iterator validity

Invalidate all pointers, iterators, and references related to x if the elements of multiset container are moved in the move constructor.

Data Races

All copied elements are accessed.

Exception Safety

No effects in case an exception is thrown.

Example 1

Let's see the simple example for default constructor:

Example

#include <iostream>
#include <set>

using namespace std;

int main(void) {
   // Default constructor
   multiset<char> s;
  
   int size = s.size(); 

   cout << "Size of multiset s = " << size;
   return 0;
}

Output:

Output

Size of multiset = 0

In the above example, s is an empty multiset therefore, size is 0.

Example 2

Let's see a simple example for range constructor:

Example

#include <iostream>
#include <set>

using namespace std;

int main(void) {
   int evens[] = {2,6,6,8,10};
  
   // Range Constructor
   multiset<int> mymultiset (evens, evens+5);  

   cout << "Size of multiset container mymultiset is : " << mymultiset.size();
   return 0;
}

Output:

Output

Size of multiset container mymultiset is: 5

In the above example, multiset mymultiset is constructed with the elements of evens.

Example 3

Let's see a simple example for copy constructor:

Example

#include <iostream>
#include <set>

using namespace std;

int main(void) {
   //Default Constructor
   multiset<int> s1;
   s1.insert(5);
   s1.insert(5);

   cout << "Size of multiset container s1 is : " << s1.size();
  
   // Copy constructor
   multiset<int> s2(s1);
   cout << "\nSize of new multiset container s2 is : " << s2.size();
   return 0;
}

Output:

Output

Size of multiset container s1 is: 2
Size of new multiset container s2 is: 2

In the above example, s2 is a copy of s1 multiset.

Example 4

Let's see a simple example for move constructor:

Example

#include <iostream>
#include <set>

using namespace std;

int main(void) {
   // Default constructor
   multiset<char> s1;
   s1.insert('x');
   s1.insert('y');
   s1.insert('y');

   cout << "Size of multiset container s1 is : " << s1.size();

   // Move constructor
   multiset<char> s2(move(s1));
   cout << "\nSize of new multiset container s2 is : " << s2.size();
   return 0;
}

Output:

Output

Size of multiset container s1 is: 3
Size of new multiset container s2 is: 3

In the above example, contents of s1 are moved to s2 multiset.

Example 5

Let's see a simple example for initializer list constructor:

Example

#include <iostream>
#include <set>
#include <string>

using namespace std;

int main() {
   // Initializer list constructor
   multiset<string> fruit {"orange", "apple", "mango", "apple", "grape"};

   cout << "Size of multiset container fruit is: " << fruit.size();
   return 0;
}

Output:

Output

Size of multiset container fruit is: 5

The above example creates a multiset fruit with string as key and initializes it with initializer_list.

Input Required

This code uses input(). Please provide values below: