在STL中,容器可以动态更改大小。分配器是负责动态内存分配/重新分配的对象。 get_allocator()用于分配内存的内存块。它返回与容器关联的分配器对象的副本。它在矢量,地图,列表,集合库中定义。
句法:
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).
下面的程序说明了该函数的工作
1. std :: vector :: get_allocator()返回与向量关联的分配器对象的副本。
// C++ program to show working
// of get_allocator function
#include
#include
using namespace std;
// Function for allocating
char* Allocate(vector arr, int size)
{
// allocate space for size(s) elements
return arr.get_allocator().allocate(size);
}
void Construct(vector 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 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 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;
}
输出:
Array elements: a b c d e f g h
2. std :: list :: get_allocator()返回与列表关联的分配器对象的副本。
// C++ program to show working
// of get_allocator function
#include
#include
using namespace std;
// Function for allocating
char* Allocate(list arr, int size)
{
// allocate space for size(s) elements
return arr.get_allocator().allocate(size);
}
void Construct(list 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 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 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;
}
输出:
Array elements: a b c d e f g h
3. std :: set :: get_allocator()返回与该集合关联的分配器对象的副本。
// C++ program to show working
// of get_allocator function
#include
#include
using namespace std;
// Function for allocating
char* Allocate(set arr, int size)
{
// allocate space for size(s) elements
return arr.get_allocator().allocate(size);
}
void Construct(set 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 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 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;
}
输出:
Array elements: a b c d e f g h
参考:http://www.cplusplus.com/reference/vector/vector/get_allocator/
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程” 。