分配器
分配器由处理的标准模板库使用分配,并且元素的释放在容器存储。 所有 STL 容器包含类型 **allocator<Type>**的模板参数, Type 表示容器元素的类型。 例如,向量类声明如下所示:
template <
class Type,
class Allocator = allocator<Type>
>
class vector
标准模板库用于分发程序提供默认实现。 在许多情况下,此默认分配程序应已足够。 有关默认分配程序的更多信息,请参见 allocator Class。
编写拥有分配器
默认分配程序使用 new 和 delete 分配和释放内存。 如果要使用内存分配一个方法,例如使用共享内存,则必须创建拥有分配器。
所有分配器用于 STL 容器必须实现以下类型定义:
const_pointer |
rebind |
const_reference |
reference |
difference_type |
size_type |
pointer |
value_type |
此外,所有分配器用于 STL 容器必须执行下列方法:
构造函数 |
deallocate |
复制构造函数 |
destroy |
析构函数 |
max_size |
address |
operator== |
allocate |
operator!= |
construct |
有关这些类型定义的更多信息和方法,请参见 allocator Class。