In this article we will be discussing the working, syntax, and examples of multimap::get_allocator() function in C++ STL.
Multimaps are the associative containers, which are similar to map containers. It also facilitates to store the elements formed by a combination of key-value and mapped value in a specific order. In a multimap container there can be multiple elements associated with the same key. The data is internally always sorted with the help of its associated keys.
multimap::get_allocator() function is an inbuilt function in C++ STL, which is defined in <map> header file. get_allocator() is used to allocate the memory chunks to a multimap container. This function returns a copy of the allocator object of the container associated with it.
An allocator is an object which is responsible for dynamically memory allocation of a container.
multi_name.get_allocator();
The function accepts no parameter.
This function returns the allocator of the container associated.
Input
int *Ptr; std::multimap<int> newmap; newmap.insert(make_pair(‘A’, 22)); newmap.insert(make_pair(‘B’, 78)); newmap.insert(make_pair(‘C’, 66)); newmap.insert(make_pair(‘D’, 81)); Ptr = mymap.get_allocator().allocate(4);
Output
ptr = A:22 B:78 C:66 D:81
#include <iostream> #include <map> using namespace std; int main(){ int arrsize; multimap<char, int> mul; pair<const char, int>* pr; pr = mul.get_allocator().allocate(15); // assign some values to array arrsize = sizeof(multimap<char, int>::value_type) * 10; cout << "Size of the allocated array is: "<< arrsize << " bytes.\n"; mul.get_allocator().deallocate(pr, 5); return 0; }
If we run the above code it will generate the following output −
Size of the allocated array is: 80 bytes.
#include <iostream> #include <map> using namespace std; int main(){ int arrsize; multimap<char, int> mul; pair<const char, int>* pr; pr = mul.get_allocator().allocate(2); // assign some values to array arrsize = sizeof(multimap<char, int>::value_type) * 5; cout << "Size of the allocated array is: "<< arrsize << " bytes.\n"; mul.get_allocator().deallocate(pr, 5); return 0; }
If we run the above code it will generate the following output −
Size of the allocated array is: 40 bytes.