C++ STL 中 map get_allocator


在這篇文章中,我們將討論 C++ STL 中 map::get_allocator() 函式的工作原理、語法和示例。

什麼是 C++ STL 中的地圖?

地圖是關聯容器,可以儲存由鍵值和已對映值組合成的元素,並按照特定順序儲存。在地圖容器中,資料始終在其關聯鍵的幫助下在內部排序。地圖容器中的值可由其唯一鍵訪問。

什麼是 map::get_allocator()?

map::get_allocator( ) 是一個函式,在 <map> 標頭檔案中。get_alloctaor() 用於獲取與地圖容器關聯的分配器物件。此函式返回給定地圖的分配器物件的副本。

語法

map_name.get_allocator(key_value k);

引數

此函式不接受引數

返回值

返回地圖的分配器物件。

示例

 即時演示

#include <bits/stdc++.h>
using namespace std;
int main() {
   map<int, int> TP;
   map<int, int>::allocator_type tp = TP.get_allocator();
   cout << "checking Is allocator Pair<int, int> : "<<
   boolalpha << (tp == allocator<pair<int, int> >());
   return 0;
}

輸出

checking Is allocator Pair<int, int> : true

示例

 即時演示

#include <bits/stdc++.h>
using namespace std;
int main(void) {
   map<char, int> TP;
   pair<const char, int>* TP_pair;
   TP_pair = TP.get_allocator().allocate(5);
   cout<<"Size after allocating is: " << sizeof(*TP_pair) * 5 << endl;
   return 0;
}

輸出

Size after allocating is: 40

更新於:2020 年 4 月 15 日

205 次瀏覽

開啟您的 職業生涯

透過完成本課程獲得認證

開始
廣告