The C++ function std::list::get_allocator() returns a copy of the allocator object associated with the list. The Allocator separates the concepts of allocation/deallocation (using allocate and deallocate) and construction/destruction (using construct and destroy).
Declaration :
list_object get_allocator() const;
Parameters :This member function does not need any parameter to be passed.
Return Type:Returns a copy of the allocator object associated with the list.
Errors and Exceptions:This member function never throws exceptions.
Time Complexity :Constant O(1).
Member Function Syntax :
Allocation: list_name.get_allocator().allocate(allocation_size) De-Allocation: list_name.get_allocator().deallocate(list_pointer,allocation_size) Construct: list_name.get_allocator().construct(&list_pointer[iterator], list_element_value) Destruct: list_name.get_allocator().destroy(&list_pointer[iterator])
Below program illustrates the std::list::get_allocator() function in C++ STL:
Code #1 :
/*CPP program to illustrate
std::list::get_allocator() function*/
#include <iostream>
#include <list>
using namespace std;
//main program
int main ()
{
// Initialize list object
std::list<int> arr;
int * point, size = 10;
// Allocate space for size(s) elements
point=arr.get_allocator().allocate(size);
// Constructing values on the array in-place
for (int i=0; i<size; ++i)
arr.get_allocator().construct(&point[i], i+1);
// Printing list elements
cout << "List Elements:";
for (int i=0; i<size; ++i)
cout << ' ' << point[i];
cout << '\n';
// Destroying object pointed by pointer in-place
for (int i=0; i<size; ++i)
arr.get_allocator().destroy(&point[i]);
// Free allocated memory
arr.get_allocator().deallocate(point,size);
return 0;
}
Output :
List Elements: 1 2 3 4 5 6 7 8 9 10