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_pointer

A type for a pointer to const.

typedef const T& const_reference

A type for a const lvalue.

typedef ptrdiff_t difference_type

A type to represent the difference of any two pointers that the allocator returns from allocate( ).

typedef T* pointer

A pointer type.

template <class U> struct rebind

Binds the allocator object to a different value type. The rebind class has a single typedef, other, which is an instance of allocator, but with U as the template parameter. The rebind template 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 ...

Get STL Pocket Reference now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.