在STL中,容器可以动态更改大小。分配器是负责动态内存分配/重新分配的对象。 get_allocator()用于分配内存的内存块。它返回与容器关联的分配器对象的副本。它在矢量,Map,列表,集合库中定义。句法:
allocator_type get_allocator() const;
使用的参数:
该成员函数不需要传递任何参数。
返回类型:
它返回与向量关联的分配器对象的副本。
错误和异常:
从不抛出异常,因此我们不需要任何try-catch。
Time-Complexity:
常数O(1)。
以下示例程序旨在说明该函数的工作
1. std::vector::get_allocator()它返回与向量关联的分配器对象的副本。
// 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;
}
输出:
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 <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;
}
输出:
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 <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;
}
输出:
Array elements: a b c d e f g h
参考:http://www.cplusplus.com/reference/vector/vector/get_allocator/
相关用法
注:本文由纯净天空筛选整理自Dibyendu Roy Chaudhuri大神的英文原创作品 get_allocator() in C++。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。