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.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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:
Which choice is better – depends on your usage patterns.