C++ Standard Library C++ STL Library

C++ map - get_allocator() Function



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

Syntax

allocator_type get_allocator() const;
allocator_type get_allocator() const noexcept;

Parameters

None.

Return Value

Returns an allocator associated with the given map.

Time Complexity

Constant i.e, Θ(1).

Example:

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

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

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

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

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

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

  return 0;
}

The output of the above code will be:

Allocated size of the array: 200 bytes.

❮ C++ <map> Library