在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++。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。