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


C++ multimap get_allocator()用法及代碼示例

multimap::get_allocator()是C++中STL中的一個函數,該函數返回與此多圖關聯的分配器對象的副本。

用法:

multimap.get_allocator()

返回值:此函數返回與此多圖關聯的分配器對象的副本。


下麵的示例說明了get_allocator()方法:

例:

// C++ program demonstrate 
// multimap::get_allocator() 
  
#include <iostream> 
#include <map> 
using namespace std; 
  
int main() 
{ 
    int psize; 
    multimap<char, int> mm; 
    pair<const char, int>* p; 
  
    // allocate an array of 5 elements 
    // using mm's allocator:
    p = mm.get_allocator().allocate(5); 
  
    // assign some values to array 
    psize = sizeof(multimap<char, int>::value_type) * 5; 
  
    cout << "The size of allocated array is "
         << psize << " bytes.\n"; 
  
    mm.get_allocator().deallocate(p, 5); 
  
    return 0; 
}
輸出:
The size of allocated array is 40 bytes.

範例2:

// C++ program to demonstrate 
// multimap::get_allocator() 
  
#include <iostream> 
#include <map> 
using namespace std; 
  
int main() 
{ 
    int psize; 
  
    multimap<char, int> mm; 
    pair<const char, int>* p; 
  
    // allocate an array of 10 elements 
    // using mm's allocator:
  
    p = mm.get_allocator().allocate(10); 
  
    // assign some values to array 
    psize = sizeof(multimap<char, int>::value_type) * 10; 
  
    cout << "The size of allocated array is "
         << psize << " bytes.\n"; 
  
    mm.get_allocator().deallocate(p, 10); 
  
    return 0; 
}
輸出:
The size of allocated array is 80 bytes.


相關用法


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