Pre-Allocating stl maps / multimaps
-
Has anybody figured out a way to preallocate a multimap?
-
Has anybody figured out a way to preallocate a multimap?
Do you mean to allocate it at compile time? Or do you mean that you know how many entries you will add, and you want to reserve the memory ahead of time? If the former, I don't believe it can be done. If the latter, I would suggest making the values pointers, e.g., map. You could potentially preallocate the memory for CFoos (simple if it is just a struct, otherwise more work). The memory for the nodes would not be preallocated, but the nodes would be fairly small.
-
Has anybody figured out a way to preallocate a multimap?
Well, I was unaware of the following article when I replied earlier. You may be well aware of it. It applies to maps, but I don't know whether multimaps are supported, I have not spent the time to check. For what it is worth: http://www.codeproject.com/vcpp/stl/PGIL.asp
-
Has anybody figured out a way to preallocate a multimap?
I think you'd have to hook in your own allocator for that. The
std::[multi]map
s are implemented as red/black binary trees IIRC, so they are very dynamic in structure, just like a linked list. Hence you don't see areserve()
method in their interfaces. hashmaps are by definition preallocated, but I'm not sure if hashmaps are part of STL yet. -- Denn du bist, was du isst! Und ihr wisst, was es ist! Es ist mein Teil...?