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.
#include <iostream>
#include <vector>
using namespace std;
char * Allocate(vector< char > arr, int size)
{
return arr.get_allocator().allocate(size);
}
void Construct(vector< char > arr,
char * point, int size)
{
for ( int iter = 0; iter < size; ++iter)
arr.get_allocator().construct(&point[iter],
iter + 97);
}
void deAllocate(vector< char > arr,
char * point, int size)
{
for ( int iter = 0; iter < size; ++iter)
arr.get_allocator().destroy(&point[iter]);
arr.get_allocator().deallocate(point, size);
}
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.
#include <iostream>
#include <list>
using namespace std;
char * Allocate(list< char > arr, int size)
{
return arr.get_allocator().allocate(size);
}
void Construct(list< char > arr,
char * point, int size)
{
for ( int iter = 0; iter < size; ++iter)
arr.get_allocator().construct(&point[iter],
iter + 97);
}
void deAllocate(list< char > arr,
char * point, int size)
{
for ( int iter = 0; iter < size; ++iter)
arr.get_allocator().destroy(&point[iter]);
arr.get_allocator().deallocate(point, size);
}
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.
#include <iostream>
#include <set>
using namespace std;
char * Allocate(set< char > arr, int size)
{
return arr.get_allocator().allocate(size);
}
void Construct(set< char > arr,
char * point, int size)
{
for ( int iter = 0; iter < size; ++iter)
arr.get_allocator().construct(&point[iter],
iter + 97);
}
void deAllocate(set< char > arr,
char * point, int size)
{
for ( int iter = 0; iter < size; ++iter)
arr.get_allocator().destroy(&point[iter]);
arr.get_allocator().deallocate(point, size);
}
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/
Share your thoughts in the comments
Please Login to comment...