C++ STL中的set::get_allocator()是一個內置函數,該函數返回與該集合關聯的分配器對象的副本。
用法:
mulset.get_allocator();
參數:該函數不接受任何參數。
返回值:此函數返回與集合關聯的分配器。
時間複雜度:O(1)。
以下是說明set::get_allocator()方法的示例:
範例1:下麵的程序顯示了如何使用集合的分配器分配7個元素的數組。
// C++ program to demonstrate 
// std::set::get_allocator 
  
#include <iostream> 
#include <set> 
  
using namespace std; 
  
void input(int* a) 
{ 
  
    for (int i = 0; i < 7; i++) 
        a[i] = i; 
} 
  
void output(int* a) 
{ 
  
    for (int i = 0; i < 7; i++) 
        cout << a[i] << " "; 
  
    cout << endl; 
} 
  
int main() 
{ 
  
    // declare set 
    set<int> mset; 
  
    // declare int pointer 
    int* arr; 
  
    cout << "size of int pointer is:"
         << sizeof(arr) << endl; 
  
    // use allocator of set to allocate array arr. 
    arr = mset.get_allocator() 
              .allocate(7); 
  
    // insert elements(numbers from 0-6) 
    // in the array 
    input(arr); 
  
    // produce output from the array 
    output(arr); 
  
    // deallocate the memory allotted previously 
    mset.get_allocator() 
        .deallocate(arr, 7); 
  
    return 0; 
}
輸出:
size of int pointer is:8 0 1 2 3 4 5 6
注:本文由純淨天空篩選整理自Kushagra7744大神的英文原創作品 set get_allocator() in C++ STL。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。
