C++ Standard Library C++ STL Library

C++ unordered_map - get_allocator() Function



The C++ unordered_map::get_allocator function returns a copy of allocator object associated with the given unordered_map.

Syntax

allocator_type get_allocator() const noexcept;

Parameters

None.

Return Value

Returns an allocator associated with the given unordered_map.

Time Complexity

Constant i.e, Θ(1).

Example:

In the example below, the unordered_map::get_allocator function returns a copy of same allocator object used by the unordered_map uMap.

#include <iostream>
#include <unordered_map>
using namespace std;
 
int main (){
  unordered_map<int, string> uMap;
  pair<const int, string> *p;

  //allocate array with a memory to store 5 
  //elements using unordered_map's allocator
  p = uMap.get_allocator().allocate(5);

  //assign some value to the array
  int psize = sizeof(unordered_map<int, string>::value_type)*5;

  cout<<"Allocated size of the array: "<<psize<<" bytes.";

  //destroy and deallocate the array
  uMap.get_allocator().deallocate(p,5);

  return 0;
}

The output of the above code will be:

Allocated size of the array: 200 bytes.

❮ C++ <unordered_map> Library