is it possible to initialize an STL map
size?
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. For instance from Boost.Container or from C++11. Or if you accept limitations of non-statefull allocators, then you could even use map from C++98/03.
Consider to use unordered_map (again from Boost or from C++11) - it takes buckets count as constructor parameter. It differs from map, in that it is based on hashing rather than on strict weak ordering.
Another option is flat_map from Boost. 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
Which choice is better - depends on your usage patterns.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With