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:
|
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
| (constructor) | Construct allocator object (public member function) |
| (destructor) | Allocator destructor (public member function) |
| address | Return address (public member function) |
| allocate | Allocate block of storage (public member function) |
| deallocate | Release block of storage (public member function) |
| max_size | Maximum size possible to allocate (public member function) |
| construct | Construct an object (public member function) |
| destroy | Destroy an object (public member function) |
本文详细介绍了C++标准库中的默认分配器allocator。allocator是STL容器使用的内存管理模型,当容器未指定特定分配器时将使用此默认分配器。文章还解释了allocator模板类的成员类型和成员函数。
1688

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



