C++ List::get_allocator() Function



The C++ std::list::get_allocator() function is used to retrieve an allocator associated with the list.

In C++, allocators are used in the standard library to handle the allocation and deallocation of elements stored in containers. Allocation is a process of requesting access to a data set. If you allocate a data set that exists, the system allows you to open the data set, and if you allocate a data set that does not exist, the system creates space for it on an available device and allows you to open that space.

Syntax

Following is the syntax of the C++ std::list::get_allocator() −

allocator_type get_allocator() const;

Parameters

  • It does not accept any parameter.

Return Value

This function returns an allocator associated with list.

Example 1

In the following program, we are using the C++ std::list::get_allocator() function to retrieve an allocator that is associated with the current list {10, 20, 30, 40, 50}.

#include<iostream> #include<list> using namespace std; int main(void) { //create a list list<int> num_list = {10, 20, 30, 40, 50}; cout<<"List elements are: "; for(int i : num_list){ cout<<i<<" "; } int *p = NULL; p = num_list.get_allocator().allocate(5); for (int i = 0; i<5; ++i) p[i] = i + 1; cout<<"\nList contains following elements"<<endl; for (int i = 0; i<5; ++i) cout<<p[i]<<" "; return 0; }

Output

Following is the output of the above program −

List elements are: 10 20 30 40 50 
List contains following elements
1 2 3 4 5

Example 2

Following is another example of the C++ ,std::list::get_allocator() function. Here, we are creating a list(type int) named num_list. Then, using the get_allocator() function, we are trying to retrieve an allocator associated with the current list.

#include<iostream> #include<list> using namespace std; int main(void) { //create a list list<int> num_list; //creating array using this list get_allocator int *arr; arr = num_list.get_allocator().allocate(5); arr[0] = 10; arr[1] = 20; arr[2] = 30; arr[3] = 40; arr[4] = 50; cout<<"List contains the following elements: "; for(int i = 0; i<5; i++) { cout<<arr[i]<<" "; } }

Output

This will generate the following output −

List contains the following elements: 10 20 30 40 50 

Example 3

In this example, we create a list(type char) named char_list with an empty value. Then, using the get_allocator() function, we are trying to get the allocator associated with this list. We use the allocate() function to specify the number of objects 5 to allocate storage for the pointer to a nearby memory location.

#include<iostream> #include<list> using namespace std; int main(void) { //create a list list<char> char_list = {}; cout<<"Size of list: "<<char_list.size()<<endl; //creating array using this list get_allocator char *char_arr; char_arr = char_list.get_allocator().allocate(3); char_arr[0] = 'a'; char_arr[1] = 'b'; char_arr[2] = 'c'; cout<<"List contains the following elements: "; for(int i = 0; i<5; i++) { cout<<char_arr[i]<<" "; } }

Output

On executing the above program, it will produce the following output −

Size of list: 0
List contains the following elements: a b c ..
Advertisements