list get_allocator in C++ STL

list::get_allocator() is an inbuilt in function in C++ STL which is used to get allocator of container list.
Syntax:

Allocator_type get_allocator()

Parameters: This function does not except any parameter.
Return value: Returns an allocator associated with list.

Below programs explains clearly the list::get_allocator() function.
Example-1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to understand
// about list getallocator method
#include <bits/stdc++.h>
  
using namespace std;
int main(void)
{
    // Creating a container of type list
    list<int> mylist;
  
    // creating a pointer of type int
    int* array;
  
    // creating array using mylist get_allocator
    array = mylist.get_allocator().allocate(3);
  
    // inserting some data into the created array
    for (int i = 0; i < 3; i++)
        array[i] = i;
  
    // printing details of the created array
    for (int i = 0; i < 3; i++)
        cout << array[i] << " ";
}

chevron_right


Output:

0 1 2

Example-2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to understand
// about list getallocator method
#include <bits/stdc++.h>
  
using namespace std;
int main(void)
{
    // Creating a container of type list
    list<string> mylist;
  
    // creating a pointer of type int
    string* array;
  
    // creating array using mylist get_allocator
    array = mylist.get_allocator().allocate(3);
  
    // inserting some data into array
    array[0] = "Geeks";
    array[1] = "For";
    array[2] = "Geeks";
  
    // printing details of array
    for (int i = 0; i < 3; i++)
        cout << array[i] << " ";
}

chevron_right


Output:

Geeks For Geeks


My Personal Notes arrow_drop_up

All powers are within you You can do anything and everything Believe in that

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.