get_allocator() in C++

In STL, containers can change size dynamically. Allocator is an object that is responsible for dynamic memory allocation/deallocation. get_allocator() is used to allocate memory chunks of memory. It returns a copy of the allocator object associated with the container. It is defined in vector, map, list, set libraries.
Syntax:

allocator_type get_allocator() const;

Parameter Used:  
This member function does not need any parameter to be passed.

Return Type: 
It returns a copy of the allocator object associated with the vector.



Errors and Exceptions:
Never throws exceptions so we don’t need any try-catch surrounding of it.

Time-Complexity: 
Constant O(1).

Below programs illustrate the working of the function

1. std::vector::get_allocator() It returns a copy of the allocator object associated with the vector.

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to show working 
// of get_allocator function
#include <iostream>
#include <vector>
  
using namespace std;
  
// Function for allocating
char* Allocate(vector<char> arr, int size)
{
  // allocate space for size(s) elements
  return arr.get_allocator().allocate(size); 
}
  
void Construct(vector<char> arr,
                      char* point, int size)
{
    for (int iter = 0; iter < size; ++iter)
      
      // construct values in-place on the array:
      arr.get_allocator().construct(&point[iter],
                                      iter + 97); 
}
  
// Function for Deallocating
void deAllocate(vector<char> arr,
                       char* point, int size)
{
    for (int iter = 0; iter < size; ++iter)
        arr.get_allocator().destroy(&point[iter]);
      
    // free allocated memory    
    arr.get_allocator().deallocate(point, size); 
}
  
// Driver code
int main()
{
    vector<char> array;
    char* pointer;
    int size = 8;
      
    pointer = Allocate(array, size);
    Construct(array, pointer, size);
  
    cout << "Array elements:  ";
    for (int iter = 0; iter < size; ++iter)
        cout << pointer[iter] << " ";
  
    deAllocate(array, pointer, size);
  
    return 0;
}

chevron_right


Output:

Array elements:  a b c d e f g h

2. std::list::get_allocator() It returns a copy of the allocator object associated with the list.

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to show working 
// of get_allocator function
#include <iostream>
#include <list>
  
using namespace std;
  
// Function for allocating
char* Allocate(list<char> arr, int size)
{
  // allocate space for size(s) elements
  return arr.get_allocator().allocate(size); 
}
  
void Construct(list<char> arr,
                      char* point, int size)
{
    for (int iter = 0; iter < size; ++iter)
      
      // construct values in-place on the array:
      arr.get_allocator().construct(&point[iter],
                                      iter + 97); 
}
  
// Function for Deallocating
void deAllocate(list<char> arr,
                       char* point, int size)
{
    for (int iter = 0; iter < size; ++iter)
        arr.get_allocator().destroy(&point[iter]);
      
    // free allocated memory    
    arr.get_allocator().deallocate(point, size); 
}
  
// Driver code
int main()
{
    list<char> array;
    char* pointer;
    int size = 8;
      
    pointer = Allocate(array, size);
    Construct(array, pointer, size);
  
    cout << "Array elements:  ";
    for (int iter = 0; iter < size; ++iter)
        cout << pointer[iter] << " ";
  
    deAllocate(array, pointer, size);
  
    return 0;
}

chevron_right


Output:

Array elements:  a b c d e f g h

3. std::set::get_allocator() It returns a copy of the allocator object associated with the set.

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to show working 
// of get_allocator function
#include <iostream>
#include <set>
  
using namespace std;
  
// Function for allocating
char* Allocate(set<char> arr, int size)
{
  // allocate space for size(s) elements
  return arr.get_allocator().allocate(size); 
}
  
void Construct(set<char> arr,
                      char* point, int size)
{
    for (int iter = 0; iter < size; ++iter)
      
      // construct values in-place on the array:
      arr.get_allocator().construct(&point[iter],
                                      iter + 97); 
}
  
// Function for Deallocating
void deAllocate(set<char> arr,
                       char* point, int size)
{
    for (int iter = 0; iter < size; ++iter)
        arr.get_allocator().destroy(&point[iter]);
      
    // free allocated memory    
    arr.get_allocator().deallocate(point, size); 
}
  
// Driver code
int main()
{
    set<char> array;
    char* pointer;
    int size = 8;
      
    pointer = Allocate(array, size);
    Construct(array, pointer, size);
  
    cout << "Array elements:  ";
    for (int iter = 0; iter < size; ++iter)
        cout << pointer[iter] << " ";
  
    deAllocate(array, pointer, size);
  
    return 0;
}

chevron_right


Output:

Array elements:  a b c d e f g h

References: http://www.cplusplus.com/reference/vector/vector/get_allocator/



My Personal Notes arrow_drop_up

Happy coding 😊

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.