Miscellaneous
This section describes allocator, auto_ptr, and bitset—class templates that don’t quite fit in the other sections.
Allocators
An allocator is an abstraction of the new and delete expressions. The standard containers use allocators to allocate and free memory, and to construct and destroy objects that reside in a container.
The standard library defines the allocator class template, which is the default allocator for every standard container. You can supply a different allocator as long as it provides the same interface as the standard allocator.
Implementing a new allocator is trickier than it seems at first and is beyond the scope of this book. This section describes how to use the standard allocator class template.
The following are the member types of allocator:
typedef const T*const_pointerA type for a pointer to
const.typedef const T&const_referenceA type for a
constlvalue.typedef ptrdiff_tdifference_typeA type to represent the difference of any two pointers that the allocator returns from
allocate( ).typedef T*pointerA pointer type.
template <class U> structrebindBinds the allocator object to a different value type. The
rebindclass has a single typedef,other, which is an instance ofallocator, but withUas the template parameter. Therebindtemplate is necessary for standard containers that allocate helper objects, such as link nodes, rather than allocating values directly. If you are not implementing a standard container, you probably don’t need to understand ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access