當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


C++ list get_allocator用法及代碼示例

清單::get_allocator()是C++ STL中的內置函數,用於獲取容器列表的分配器。
用法:

Allocator_type get_allocator()

參數:此函數沒有任何參數。
返回值:返回與列表關聯的分配器。

下麵的程序清楚地說明了列表:: get_allocator()函數。示例1:



// C++ program to understand 
// about list getallocator method 
#include <bits/stdc++.h> 
  
using namespace std; 
int main(void) 
{ 
    // Creating a container of type list 
    list<int> mylist; 
  
    // creating a pointer of type int 
    int* array; 
  
    // creating array using mylist get_allocator 
    array = mylist.get_allocator().allocate(3); 
  
    // inserting some data into the created array 
    for (int i = 0; i < 3; i++) 
        array[i] = i; 
  
    // printing details of the created array 
    for (int i = 0; i < 3; i++) 
        cout << array[i] << " "; 
}
輸出:
0 1 2

示例2:

// C++ program to understand 
// about list getallocator method 
#include <bits/stdc++.h> 
  
using namespace std; 
int main(void) 
{ 
    // Creating a container of type list 
    list<string> mylist; 
  
    // creating a pointer of type int 
    string* array; 
  
    // creating array using mylist get_allocator 
    array = mylist.get_allocator().allocate(3); 
  
    // inserting some data into array 
    array[0] = "Geeks"; 
    array[1] = "For"; 
    array[2] = "Geeks"; 
  
    // printing details of array 
    for (int i = 0; i < 3; i++) 
        cout << array[i] << " "; 
}
輸出:
Geeks For Geeks



相關用法


注:本文由純淨天空篩選整理自ankit15697大神的英文原創作品 list get_allocator in C++ STL。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。