#include <iostream>
#include <map>
using namespace std;
// 函数Get_allocator用于返回map/multimap的内存配置器,
// 内存配置器类似指针的首地址,用于指明对象的初始存储位置,
// 获取容器的内存分配器至关重要
void main()
{
typedef map<int, double> MAP;
// 定义allocator容器配置器
MAP::allocator_type m1_alloc; //map<int, double>::allocator_type ma_alloc;
MAP::allocator_type m2_alloc;
MAP::allocator_type m3_alloc;
MAP::allocator_type m4_alloc;
// 定义容器(map容器类对象),get_allocator为成员函数
MAP m1, m2, m3;
m1_alloc = m1.get_allocator();// 原型:allocator_type get_allocator() const
m2_alloc = m2.get_allocator();
m3_alloc = m3.get_allocator();
// 把m1的容器配置器存储m4
MAP m4(less<int>(), m1_alloc);
// 获取m4的容器配置器
m4_alloc = m4.get_allocator();
// 检查m4和m1的容器配置器是否相同
if (m4_alloc == m2_alloc)
{
cout << "配置器相同" << endl;
}
else
cout << "配置器不相同" << endl;
cin.get();
}
本文通过示例代码详细介绍了如何使用C++标准库中的map容器的get_allocator成员函数来获取内存配置器,并展示了如何比较不同容器实例之间的内存配置器是否相同。
396

被折叠的 条评论
为什么被折叠?



