map::get_allocator function


The map::get_allocator function returns an allocator object used by the map container.

allocator_type get_allocator() const;

allocator_type – An alias of allocator< pair<Key, T> > .
Key -Type of the first value.
T -Type of the second value.

Link: Read more about allocator class template here

Parameters:
none

Return type
allocator_type -A copy of the allocator object used by the container.

Since allocator class template handles the memory management of the STL containers ,we can say get_allocator() returns the memory model of the container.

map<double,char> mp{{2.30 , ‘@’} , { 5.65 , ‘%’} , { 9.08 , ‘(‘}};

allocator< pair<double,char> > al=mp.get_allocator(); //returns an object of mp allocator

pair<double,char> *pt=al.allocate(3);

al.construct( pt , pair<double,char>({3.4 , ‘#’}) );
al.construct( pt+1 , pair<double,char>({ 7.67 , ‘&’}) );
al.construct( pt+1 , pair<double,char>({ 8.9 , ‘*’}) );

cout<< pt[0].first << ” ” << pt[0].second << endl
 << pt[1].first << ” ” << pt[1].second << endl
 << pt[2].first << ” ” << pt[2].second << endl ;

al.deallcoate(pt,3); //do not forget! deletes the storage

Output

3.4 #
7.67 &
8.9 *

Here is another example to test if the memory model of two containers are similar.

map<int,char> m1{{1,’1′},{2,’2′}} ,
m2{{90 , ‘_’ }};

if( m1.get_allocator( ) == m2.get_allocator( ) )
{
 cout<< “m1 and m2 memory model are similar.\n”;
}
else
cout<<“m1 and m2 memory model are not similar.”;

Output

mp and mp1 memory model are similar.


 


Stop wasting time,earn money($$$) from your website-Join Now!