C++ multiset Library - Range Constructor



Description

The multiset::multiset() range constructor in C++ STL creates a multiset by copying elements from a specified range [first, last). It initializes one multiset with elements from another container or array. It automatically sorts the elements according to the comparison function. By default, std::less is used.

Syntax

Following is the syntax of multiset range constructor −

template <class InputIterator>
multiset(InputIterator first, InputIterator last,
         const Compare& comp = Compare(),
         const Allocator& alloc = Allocator());  // C++98
template <class InputIterator>
multiset(InputIterator first, InputIterator last,
         const Allocator& alloc = Allocator());  // C++14

Return value

The range constructor does not return any value since it is a constructor.

Exceptions

It may throw exceptions if element construction, comparison, or memory allocation fails.

Time complexity

The time complexity of multiset range constructor is O(N log N), where N is the number of elements in the range.

Examples of multiset Range Constructor

The following examples demonstrate the usage of multiset range constructor

Creating a multiset from an Array

In this example, we have created a multiset by copying elements from an array. We have used two iterators to define the range that points to the beginning and end of the array.

#include <iostream>
#include <set>
using namespace std;

void printMultiset(const multiset<int>& numbers) {
    for (auto it = numbers.begin(); it != numbers.end(); ++it)
        cout << " " << *it;
    cout << endl;
}

int main() {
    int arr[] = {50, 20, 30, 20, 10, 50, 40};
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << "Array size: " << n << endl;
    cout << "Array elements:";
    for (int i = 0; i < n; i++)
        cout << " " << arr[i];
    cout << endl;

    // Using index 1 to 4 of array
    multiset<int> numbers(arr + 1, arr + 5);
    cout << "\nSize of multiset: " << numbers.size() << endl;
    cout << "Multiset elements:";
    printMultiset(numbers);

    return 0;
}

The output of the above code is given below −

Array size: 7
Array elements: 50 20 30 20 10 50 40

Size of multiset: 4
Multiset elements: 10 20 20 30

Creating a multiset from a Vector

Here is an example of using the range constructor to create a multiset from a vector

#include <iostream>
#include <set>
#include <vector>
using namespace std;

int main() {
    vector<string> str = {"apple", "banana", "apple", 
                          "cherry", "banana", "apple"};
    cout << "Given vector elements:";
    for (const auto &s : str)
        cout << " " << s;
    cout << endl;

    multiset<string> word(str.begin(), str.end());
    cout << "Multiset elements:";
    for (const auto &fruit : word)
        cout << " " << fruit;

    return 0;
}

The output of the above code is given below −

Given vector elements: apple banana apple cherry banana apple
Multiset elements: apple apple apple banana banana cherry

Converting Data Type Using Range Constructor

The following example converts the double data type of vector elements into an int multiset elements −

#include <iostream>
#include <set>
#include <vector>
using namespace std;

int main() {
    vector<double> decimals = {2.5, 3.1, 4.7, 1.2};
    cout << "Given vector: ";
    for (double d : decimals)
        cout << d << " ";
    cout << endl;

    multiset<int> nums(decimals.begin(), decimals.end());
    cout << "\nConverted multiset: ";
    for (int x : nums)
        cout << x << " ";
}

The output of the above code is given below −

Given vector: 2.5 3.1 4.7 1.2 

Converted multiset: 1 2 3 4 
multiset.htm
Advertisements