编写自己的MemoryAllocator

本文详细介绍了C++标准库中的默认分配器allocator。allocator是STL容器使用的内存管理模型,当容器未指定特定分配器时将使用此默认分配器。文章还解释了allocator模板类的成员类型和成员函数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

allocator

class template
<memory>
template <class T> class allocator;

Default allocator

Allocators are classes that define memory models to be used by some parts of the Standard Library, and most specifically, by  STL containers .

This section describes the  default allocator template   allocator  (lowercase). This is the allocator that all standard containers will use if their last (and optional) template parameter is not specified, and is the only predefined allocator in the standard library.

Other allocators may be defined. Any class having the same members as this  default allocator  and following its minimum requirements can be used as an allocator -- notice that only under very specific circumstances this is needed.

Technically, a memory model described by allocators might be specialized for each type of object to be allocated and even may store local data for each container they work with. Although this does not happen with the default  allocator .

The class template of  allocator  is declared as:
 
template < class T > class allocator;

Taking one template parameter (which is assumed to be  T  in this entire reference).

Member types

member definition in allocator represents
value_type T Element type
pointer T* Pointer to element
reference T& Reference to element
const_pointer const T* Constant pointer to element
const_reference const T& Constant reference to element
size_type size_t Quantities of elements
difference_type ptrdiff_t Difference between two pointers

Member functions

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值