Skip to content
Related Articles

Related Articles

Improve Article

get_allocator() in C++

  • Last Updated : 01 Jun, 2018

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.




// 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;
}
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.




// 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;
}
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.




// 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;
}
Output:
Array elements:  a b c d e f g h

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

Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.



My Personal Notes arrow_drop_up
Recommended Articles
Page :