Share via


Allocators

Allocators 會使用標準樣板程式庫來處理配置及解除配置容器中的項目存放區。 所有的 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

請參閱

參考

標準樣板程式庫