Haro Panosyan wrote:
Victor Bazarov wrote:
Haro Panosyan wrote:
Seems there is no reserve() function defined for STL map.
If so, could someone please explain why?
Same goes with resize().
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.
I would assume same as with vector.
Why would you assume that? 'map' has nothing really in common 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.
Why do I have to ask before you start thinking that "this should be
something not logical"? Why don't you think that when you just look
at the interface of 'map'? If it's not there, it probably wasn't very
logical to have it there.
Now that we've come to the conclusion that the absence of 'reserve'
and 'resize' in 'map' is due to them being "not logical" for 'map',
we could try to find out *why* that is. I proposed to try to prove
the illogicality of them by starting from the opposite end: let's
_assume_ that they need to be there because that's logical (again,
we're just _assuming_ it is, for the proof's sake). If they should
be there, what would they do and how would they do it? You can take
'vector's members 'reserve' or 'resize' as the guideline (as _you_
so kindly proposed).
The 'resize' member of 'vector', for example, changes the size of
the collection by either trimming it from the end or by adding some
elements at the end. Trimming is simple. We could, I suppose, trim
a map just as well, since the order of elements in it is well-known
and determined by the keys. But let's look at adding some elements
to the end. 'vector' does that by default-constructing or copy-
constructing them. How would map do it? Default-constructing of
a stored value puts some value into "key" part, right? Now, if we
resize by adding 10 values, would all "keys" be the same? Yes, of
course, they are copy-constructed, don't they? But wait, 'map' has
only _one_ element with any particular "key", doesn't it? So, adding
10 elements of the same value would result in fact in adding _at_most_
1. One! Or none, if such element already exists in the map. So, it
seems that resizing by 10 elements wouldn't really work as anybody
might expect... Or would it?...
V