O'Reilly logo

STL Pocket Reference by Ray Lischner

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required