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
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP