C++ multiset Library - get_allocator() Function



Description

The multiset::get_allocator() function in C++ STL is used to return a copy of the allocator object that is associated with the multiset container. This allocator object is used to handle the memory allocation and deallocation for the multiset.

Syntax

Following is the syntax of multiset::get_allocator() function −

allocator_type get_allocator() const;

Parameters

The get_allocator() function does not accept any parameter.

Return value

The get_allocator() function returns the allocator object associated with the multiset container.

Exceptions

If an exception is thrown, the container remains unchanged.

Time complexity

The time complexity of multiset::get_allocator() function is O(1).

Examples of multiset::get_allocator() Function

The following examples demonstrate the usage of multiset::get_allocator() function in multiset −

Allocating Multiple Elements Using get_allocator()

In this example, we have used the get_allocator() function to get the allocator from multiset nums and allocated memory for 5 integer elements.

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

int main() {
    multiset<int> nums = {10, 20, 30};
    cout << "Multiset elements: ";
    for (const auto &num : nums)
        cout << " " << num;
    cout << endl;

    // Get the allocator
    auto alloc = nums.get_allocator();

    // Allocate memory for 5 integers
    int *p = alloc.allocate(5);

    // Construct values in allocated memory
    for (int i = 0; i < 5; i++)
        alloc.construct(p + i, i * 10);

    cout << "Allocated numbers: ";
    for (int i = 0; i < 5; i++)
        cout << p[i] << " ";
    cout << endl;

    // Destroy and deallocate
    for (int i = 0; i < 5; i++)
        alloc.destroy(p + i);
    alloc.deallocate(p, 5);

    return 0;
}

The output of the above code is given below −

Multiset elements:  10 20 30
Allocated numbers: 0 10 20 30 40 

Using get_allocator() with String Multiset

In this example, we have used the get_allocator() function to get the allocator from a string multiset words and allocated memory for 5 strings.

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

int main() {
    multiset<string> words = {"apple", "banana", "cherry"};
    cout << "Multiset elements:";
    for (const auto &word : words)
        cout << " " << word;
    cout << endl;

    // Get the allocator
    auto alloc = words.get_allocator();

    // Allocate memory for 5 strings
    string *p = alloc.allocate(5);

    // Construct values in allocated memory
    alloc.construct(p, "one");
    alloc.construct(p + 1, "two");
    alloc.construct(p + 2, "three");
    alloc.construct(p + 3, "four");
    alloc.construct(p + 4, "five");

    cout << "Allocated strings:";
    for (int i = 0; i < 5; i++)
        cout << " " << p[i];
    cout << endl;

    // Destroy and deallocate
    for (int i = 0; i < 5; i++)
        alloc.destroy(p + i);
    alloc.deallocate(p, 5);

    return 0;
}

The output of the above code is given below −

Multiset elements: apple banana cherry
Allocated strings: one two three four five
multiset.htm
Advertisements