如何在C ++ 11中的内存池中分配std :: map的内部RB_tree节点?

问题描述 投票:2回答:1

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定义,我们可以为其KeyValue提供定制的分配器。

问题:

  1. [在大多数情况下,std::map由RB树实现。我们如何为RB树节点提供定制的分配器?恐怕我们做不到。
  2. 如果第一个问题的答案是我们可以NOT为RB树节点提供定制的分配器,我们可以NOT为其他基于节点的容器提供定制的分配器(例如,std::lis t和std::set)。请确认我的理解是否正确。
c++ c++11 stdmap allocator
1个回答
2
投票

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;

-3
投票

您无法更改容器管理其内存的方式。但是,可以使用std::map使用容器在适当的位置构造对象。

© www.soinside.com 2019 - 2024. All rights reserved.