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 allocator used by the container.
Return Value: It returns the allocator object used to construct the container.
Exceptions: In this method, an Exception is thrown if any element comparison object throws an exception.
The below program illustrates the unordered_set::get_allocator() function
Program 1:
C++
#include <iostream>
#include <unordered_set> // Their Library
using namespace std;
int main()
{
unordered_set< int > c;
unordered_set< int >::allocator_type a
= c.get_allocator();
cout << "Is allocator Pair<int, int> : " << boolalpha
<< (a == allocator<pair< int , int > >());
return 0;
}
|
Output
Is allocator Pair<int, int> : true
Complexity: It takes constant(O(1)) time of complexity to perform an operation.
Program 2 :
C++
#include <iostream>
#include <unordered_map> // Their Library
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;
}
|
Output
Allocated size = 64
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!