在这里有一个比较不太容易理解的东西:rebind。C++标准里这么描述rebind的: The member class template rebind in the table above is effectively a typedef template: if the name Allocator is bound to SomeAllocator<T>, then Allocator::rebind<U>::other is the same type as SomeAllocator<U>. 啥意思?可以用一个简单的例子来说明下: 学校都学过数据结构,比方说栈、单向列表、树。我们就拿栈和列表来对比,看看有什么大不一样的地方。撇开数据结构上的差异,从allocator的角度来看,我们可以发现:堆栈是存贮元素本身的,但是列表实际上不是直接存储元素本身的。要维护一个列表,我们至少还需要一个所谓的next的指针。因此,虽然是一个保存int的列表list<int>,但是列表存储的对象并不是int本身,而是一个数据结构,它保存了int并且还包含指向前后元素的指针。那么,list<int, allocator<int>>如何知道分配这个内部数据结构呢?毕竟allocator<int>只知道分配int类型的空间。这就是rebind要解决的问题。通过allocator<int>::rebind<_Node>()你就可以创建出用于分配_Node类型空间的分配器了。 接下来要提供其他的接口。根据The default allocator的描述,我们要提供如下一些接口: pointer address(reference val) const const_pointer address(const_reference val) const 返回val的地址 pointer allocate(size_type cnt, CHxAllocator<void>::const_pointer pHint = 0) 分配空间。类似malloc。pHint可以无视,主要是给类库使用,用于提高性能。 void deallocate(pointer p, size_type n) 释放空间,类似free。 size_type max_size() const throw() 可分配的最大数量。 void construct(pointer p, const_reference val) 在地址p所指向的空间,使用val进行填充。需要使用到palcement new,以便保证调用到构造函数。 void destroy(pointer p) 析构p指向的内存块中内容。一般通过显示调研析构函数来执行。