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
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
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
multiset (const multiset& x); //until C++ 11
multiset (const multiset& x);
multiset (const multiset& x, const allocator_type& alloc); //since C++ 11
move constructor
multiset (multiset&& x);
multiset (multiset&& x, const allocator_type& alloc); //since C++ 11
initializer list constructor
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
A comparison function object is utilized to compare two key arguments and determine if the first argument precedes the second argument, returning true in that case, or false otherwise. By default, it employs the less<key_type> predicate.
The alloc object is utilized for handling all memory allocations within this container.
Begin by setting the input iterator to the initial position within the specified range.
The final parameter is an input iterator pointing to the last position within a specified range.
x : Another multiset object of the same type.
An initializer list object is utilized for copying the elements.
Return value
Constructor never returns any value.
Complexity
For blank constructors and move constructors, the complexity will remain constant.
For any other scenarios, the computational complexity will increase linearly based on the gap between the iterators assuming the elements are already arranged in order.
Iterator validity
Invalidate all pointers, iterators, and references associated with x when the elements of the multiset container are transferred 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 explore a basic example demonstrating the use of a default constructor:
#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:
Size of multiset = 0
In the aforementioned example, "s" represents an empty multiset, resulting in a size of 0.
Example 2
Let's see a simple example for range constructor:
#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:
Size of multiset container mymultiset is: 5
In the previous example, the multiset named mymultiset is built using the elements from the evens set.
Example 3
Let's see a simple example for copy constructor:
#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:
Size of multiset container s1 is: 2
Size of new multiset container s2 is: 2
In the previous instance, s2 represents a duplicate of s1 multiset.
Example 4
Let's see a simple example for move constructor:
#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:
Size of multiset container s1 is: 3
Size of new multiset container s2 is: 3
In the example provided, the contents of set 1 are transferred to multiset 2.
Example 5
Let's examine a basic illustration of an initializer list constructor:
#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:
Size of multiset container fruit is: 5
The previous instance generates a multiset of fruits with a string as the key and initializes it using an initializer list.