map get_allocator in C++ STL

C++Server Side ProgrammingProgramming

In this article we will be discussing the working, syntax and examples of map::get_allocator() function in C++ STL.

What is Map in C++ STL?

Maps are the associative container, which facilitates to store the elements formed by a combination on key value and mapped value in a specific order. In a map container the data is internally always sorted with the help of its associated keys. The values in map container is accessed by its unique keys.

What is map::get_allocator()?

The map::get_allocator( ) is a function which comes under <map> header file. get_alloctaor() is used to get the allocator object which is associated with the map container. This function returns the copy of the allocator object of the given map.

Syntax

map_name.get_allocator(key_value k);

Parameters

This function accepts no parameter

Return value

It returns an allocator object of the map.

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
int main() {
   map<int, int> TP;
   map<int, int>::allocator_type tp = TP.get_allocator();
   cout << "checking Is allocator Pair<int, int> : "<<
   boolalpha << (tp == allocator<pair<int, int> >());
   return 0;
}

Output

checking Is allocator Pair<int, int> : true

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
int main(void) {
   map<char, int> TP;
   pair<const char, int>* TP_pair;
   TP_pair = TP.get_allocator().allocate(5);
   cout<<"Size after allocating is: " << sizeof(*TP_pair) * 5 << endl;
   return 0;
}

Output

Size after allocating is: 40
raja
Published on 15-Apr-2020 12:08:29
Advertisements