在C ++ STL中映射get_allocator
在本文中,我们将讨论C++STL中map::get_allocator()函数的工作,语法和示例。
什么是C++STL中的Map?
映射是关联容器,它有助于按特定顺序存储键值和映射值的组合所形成的元素。在映射容器中,数据始终在内部借助其关联的键进行排序。映射容器中的值通过其唯一键访问。
什么是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
