unordered_set get_allocator() in C++ STL with Examples

The get_allocator() method of unordered_set is the part of Standard Template Library(STL) of C++. This method gets the stored allocator object and returns it.

Syntax:

Allocator_type get_allocator() const;

where allocator_type is the type of the allocator used by the container.

Return Value: It returns the allocator object used to construct the container.

Exceptions: In this method, Exception is thrown if any element comparison object throws exception.

Below program illustrate the unordered_set::get_allocator() function

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to illustrate
// unordered_set get_allocator()
  
#include <iostream>
#include <unordered_set>
  
using namespace std;
int main()
{
  
    //'c' is object of 'unordered_set'
    unordered_set<int> c;
  
    //'allocator_type' is inherit in 'unordered_set'
    //'a' is object of 'allocator_type'
    //'get_allocator()' is member of 'unordered_set'
    unordered_set<int>::allocator_type a = c.get_allocator();
  
    // Comparing the Allocator with Pair<int, int>
    cout << "Is allocator Pair<int, int> : "
         << boolalpha
         << (a == allocator<pair<int, int> >());
  
    return 0;
}

chevron_right


Output:

Is allocator Pair : true

Complexity :
It takes constant(O(1)) time of complexity to perform an operation.

Program 2 :

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to illustrate
// unordered_set get_allocator()
  
#include <iostream>
#include <unordered_map>
  
using namespace std;
  
int main(void)
{
    unordered_map<char, int> um;
    pair<const char, int>* a;
  
    a = um.get_allocator().allocate(8);
  
    cout << "Allocated size = " << sizeof(*a) * 8 << endl;
  
    return 0;
}

chevron_right


Output:

Allocated size = 64


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.