Open In App

map get_allocator in C++ STL

Last Updated : 11 Jul, 2025
Summarize
Comments
Improve
Suggest changes
Share
Like Article
Like
Report
map::get_allocator() is a built in function in C++ STL which is used to get allocator of container map. Syntax:
Allocator_type get_allocator()
Parameters: This function does not accept any parameter. Return value: Returns an allocator associated with map. Below programs explains clearly the map::get_allocator() function. Example-1: CPP
// CPP program to illustrate
// map get_allocator()
#include <bits/stdc++.h>
using namespace std;
int main()
{

    //'mp' is object of 'map'
    map<int, int> mp;

    //'allocator_type' is inherit in 'map'
    //'m' is object of 'allocator_type'
    map<int, int>::allocator_type m = mp.get_allocator();

    // Comparing the Allocator with Pair<int, int>
    cout << "Is allocator Pair<int, int> : "
         << boolalpha
         << (m == allocator<pair<int, int> >());

    return 0;
}
Output:
Is allocator Pair : true
Example-2: CPP
// CPP program to illustrate
// map get_allocator()
#include <bits/stdc++.h>
using namespace std;

int main(void)
{
    map<char, int> m;
    pair<const char, int>* a;

    a = m.get_allocator().allocate(8);

    cout << "Allocated size = " << sizeof(*a) * 8 << endl;

    return 0;
}
Output:
Allocated size = 64

Article Tags :
Practice Tags :

Similar Reads