std::map
定义复制到下面:
template<
class Key,
class T,
class Compare = std::less<Key>,
class Allocator = std::allocator<std::pair<const Key, T> >
> class map;
基于std::map
定义,我们可以为其Key和Value提供定制的分配器。
问题:
std::map
由RB树实现。我们如何为RB树节点提供定制的分配器?恐怕我们做不到。std::lis
t和std::set
)。请确认我的理解是否正确。IIRC,您作为map
模板参数提供的分配器类型也用于分配节点(即其allocate
成员函数)。例如,您可以在source code of libstdc++:]中进行观察
typedef typename __gnu_cxx::__alloc_traits<_Alloc>::template
rebind<value_type>::other _Pair_alloc_type;
typedef _Rb_tree<key_type, value_type, _Select1st<value_type>,
key_compare, _Pair_alloc_type> _Rep_type;
您无法更改容器管理其内存的方式。但是,可以使用std::map
使用容器在适当的位置构造对象。