set get_allocator() in C++ STL

The set::get_allocator() in C++ STL is an in-built function which returns the copy of the allocator object associated with the set.

Syntax:

mulset.get_allocator();

Parameters: This function does not accept any parameters.

Return Value: This function returns the allocator associated with the set.

Time Complexity: O(1).

Below are the examples to illustrate set::get_allocator() method:

Example 1: The program below shows how allocator of a set can be used to allocate an array of 7 elements.

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to demonstrate
// std::set::get_allocator
  
#include <iostream>
#include <set>
  
using namespace std;
  
void input(int* a)
{
  
    for (int i = 0; i < 7; i++)
        a[i] = i;
}
  
void output(int* a)
{
  
    for (int i = 0; i < 7; i++)
        cout << a[i] << " ";
  
    cout << endl;
}
  
int main()
{
  
    // declare set
    set<int> mset;
  
    // declare int pointer
    int* arr;
  
    cout << "size of int pointer is: "
         << sizeof(arr) << endl;
  
    // use allocator of set to allocate array arr.
    arr = mset.get_allocator()
              .allocate(7);
  
    // insert elements(numbers from 0-6)
    // in the array
    input(arr);
  
    // produce output from the array
    output(arr);
  
    // deallocate the memory allotted previously
    mset.get_allocator()
        .deallocate(arr, 7);
  
    return 0;
}

chevron_right


Output:

size of int pointer is: 8
0 1 2 3 4 5 6


My Personal Notes arrow_drop_up


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.