"Haro Panosyan" <haro@ti.com> wrote in message
news:de2o5u$c0h$1@home.itg.ti.com...[color=blue]
> Victor Bazarov wrote:[color=green]
>> Haro Panosyan wrote:
>>[color=darkred]
>>> Seems there is no reserve() function defined for STL map.
>>> If so, could someone please explain why?
>>> Same goes with resize().[/color]
>>
>>
>> What would 'reserve' do? What would 'resize' do? As soon as you
>> answer those, I will help you prepare a proposal for a change in
>> the library specification.[/color]
>
> I would assume same as with vector.
> But since you are asking, then this should be
> something not logical and this is what I don't understand,
> hence my original question was someone to explain.[/color]
It's because vector is like this
(1)(2)(3)(4)(5)(6)
All in a row, right? But map actually looks something like this:
(1)
(3) (4)
(7) (2) (6) (5)
Where the digits are just the order in which you inserted the elements. As
you can see, the data is not contiguous and the ideal thing for reserve to
do is not the same in all cases.
If your goal is to acheive a minimum number of allocation calls, you can
provide an allocator to the template for map [1]. [2] talks about how to
write an allocator. You might want to consider writing one that just
allocates blocks of a certain size, where that size is the most that you
would use. Then you could get all of the allocation over with quickly.
Alternately, you could write one that allocates blocks of sizes that
correspond to powers of two, which often reduces problems related to memory
fragmentation.
Hope this helps.
JFA1
[1]
http://msdn.microsoft.com/library/de...fmap_class.asp
[2]
http://www.codeproject.com/cpp/allocator.asp