C++官网参考链接:https://cplusplus.com/reference/map/map/get_allocator/
公有成员函数
<map>
std::map::get_allocator
C++98
allocator_type get_allocator() const;
C++11
allocator_type get_allocator() const noexcept;
获取allocator
返回与map关联的allocator对象的副本。
形参
没有形参。
返回值
allocator。
成员类型allocator_type是容器使用的allocator的类型,在map中定义为其第四个模板形参(Alloc)的别名。
用例
// map::get_allocator
#include <iostream>
#include <map>
int main ()
{
int psize;
std::map<char,int> mymap;
std::pair<const char,int>* p;
// allocate an array of 5 elements using mymap's allocator:
p=mymap.get_allocator().allocate(5);
// assign some values to array
psize = sizeof(std::map<char,int>::value_type)*5;
std::cout << "The allocated array has a size of " << psize << " bytes.\n";
mymap.get_allocator().deallocate(p,5);
return 0;
}
该示例展示了一种使用容器使用的相同allocator为pair数组分配内存的详细方法。
一个可能的输出是:
复杂度
常量。
iterator的有效性
没有变化。
数据竞争
访问容器。
不访问包含的元素:同时访问或修改它们是安全的。
异常安全
无抛出保证:该成员函数从不抛出异常。
复制默认allocator(default allocator)的任何实例化也保证永远不会抛出。