C++ STL 中的 multimap get_allocator() 函式
在本文中,我們將討論 C++ STL 中 multimap::get_allocator() 函式的工作原理、語法和示例。
什麼是 C++ STL 中的 Multimap?
Multimap 是關聯容器,類似於 map 容器。它還可以按照特定順序儲存由鍵值對和對映值組合而成的元素。在 multimap 容器中,可以存在多個與同一鍵關聯的元素。資料在內部始終藉助其關聯的鍵進行排序。
什麼是 multimap::get_allocator()?
multimap::get_allocator() 函式是 C++ STL 中的內建函式,它在 <map> 標頭檔案中定義。get_allocator() 用於為 multimap 容器分配記憶體塊。此函式返回與其關聯的容器的分配器物件的副本。
分配器是一個負責容器動態記憶體分配的物件。
語法
multi_name.get_allocator();
引數
此函式不接受任何引數。
返回值
此函式返回關聯容器的分配器。
輸入
int *Ptr; std::multimap<int> newmap; newmap.insert(make_pair(‘A’, 22)); newmap.insert(make_pair(‘B’, 78)); newmap.insert(make_pair(‘C’, 66)); newmap.insert(make_pair(‘D’, 81)); Ptr = mymap.get_allocator().allocate(4);
輸出
ptr = A:22 B:78 C:66 D:81
示例
#include <iostream>
#include <map>
using namespace std;
int main(){
int arrsize;
multimap<char, int> mul;
pair<const char, int>* pr;
pr = mul.get_allocator().allocate(15);
// assign some values to array
arrsize = sizeof(multimap<char, int>::value_type) * 10;
cout << "Size of the allocated array is: "<< arrsize << " bytes.\n";
mul.get_allocator().deallocate(pr, 5);
return 0;
}輸出
如果我們執行上面的程式碼,它將生成以下輸出:
Size of the allocated array is: 80 bytes.
示例
#include <iostream>
#include <map>
using namespace std;
int main(){
int arrsize;
multimap<char, int> mul;
pair<const char, int>* pr;
pr = mul.get_allocator().allocate(2);
// assign some values to array
arrsize = sizeof(multimap<char, int>::value_type) * 5;
cout << "Size of the allocated array is: "<< arrsize << " bytes.\n";
mul.get_allocator().deallocate(pr, 5);
return 0;
}輸出
如果我們執行上面的程式碼,它將生成以下輸出:
Size of the allocated array is: 40 bytes.
廣告
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C 語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP