简体   繁体   English

初始化STL`map`大小

[英]initialize an STL `map` size

is it possible to initialize an STL map size? 是否可以初始化STL map大小?

I know how many elements will be in my map at the end and I want to allocate all the required memory at the very beginning. 我知道最后我的地图中会有多少元素,我想在一开始就分配所有必需的内存。

There are several options: 有几种选择:

  • You may try to use map with statefull allocator. 您可以尝试使用带有statefull分配器的map。 For instance from Boost.Container or from C++11. 例如,来自Boost.Container或来自C ++ 11。 Or if you accept limitations of non-statefull allocators, then you could even use map from C++98/03. 或者,如果您接受非statefull分配器的限制,那么您甚至可以使用C ++ 98/03中的map。

  • Consider to use unordered_map (again from Boost or from C++11) - it takes buckets count as constructor parameter. 考虑使用unordered_map(同样来自Boost或C ++ 11) - 它需要将桶计为构造函数参数。 It differs from map, in that it is based on hashing rather than on strict weak ordering. 它与map不同,因为它基于散列而不是严格的弱排序。

  • Another option is flat_map from Boost . 另一个选项是来自Boost的 flat_map。 It has reserve member function. 它具有备用成员功能。 Description of flat map/set: 平面地图/集的描述

Boost.Container flat_[multi]map/set containers are ordered-vector based associative containers based on Austern's and Alexandrescu's guidelines Boost.Container flat_ [multi] map / set容器是基于Austern和Alexandrescu指南的基于有序矢量的关联容器

  • Or, if boost is not available - then you could simply use std::vector + std::sort + std::lower_bound (or wrap them to small flat_map like class). 或者,如果boost不可用 - 那么你可以简单地使用std :: vector + std :: sort + std :: lower_bound(或将它们包装到类似的小flat_map)。 Ie just put your elements to vector (unordered), then sort it, and then - when you need to query element by key - just use lower_bound. 即只是将您的元素放入向量(无序),然后对其进行排序,然后 - 当您需要按键查询元素时 - 只需使用lower_bound。

Which choice is better - depends on your usage patterns. 哪种选择更好 - 取决于您的使用模式。

You can't. 你不能。 It's a tree (usually a red-black tree). 它是一棵树(通常是一棵红黑树)。 The actual values are going to determine memory layout. 实际值将决定内存布局。

However , you can 但是 ,你可以

  • use Boost Intrusive maps (which use elements you allocated in another container, like a vector), decorated with 'hooks' to implement the map functionality over it 使用Boost Intrusive贴图(使用你在另一个容器中分配的元素,如矢量),用'hooks'装饰以在其上实现贴图功能

  • use std::map with allocators, so you can allocate all the actual elements from a fixed 'pool' (memory region) 将std :: map与allocator一起使用,这样就可以从固定的'pool'(内存区域)中分配所有实际元素

The only thing I can think of is to use its iterator constructors. 我唯一能想到的是使用它的迭代器构造函数。 The only trick there is that then you have to create another container with the size you need, and give its iterators to the constructor. 唯一的技巧是,你必须创建另一个具有所需大小的容器,并将其迭代器提供给构造函数。

reserve can be emulated by rehash as in Table 103 in N3376 . 可以通过rehash模拟reserve ,如N3376中的表103 所示

a.rehash(n) 
Post: a.bucket_count() > a.size() / a.max_load_factor() 
      and a.bucket_count() >= n.

a.reserve(n) Same as a.rehash(ceil(n / a.max_load_factor()))

Source 资源

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM