473,320 Members | 1,802 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

STL map

Seems there is no reserve() function defined for STL map.
If so, could someone please explain why?
Same goes with resize().

Thanks,
-haro
Aug 18 '05 #1
8 3507
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.

V
Aug 18 '05 #2
>Seems there is no reserve() function defined for STL map.
If so, could someone please explain why?
Same goes with resize().


These operations don't really make much sense for map objects, which
don't necessarily use a structure where reserving space in advance
makes sense (e.g. balanced trees).

Aug 18 '05 #3
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.
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.


V

Aug 18 '05 #4
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
Aug 18 '05 #5

"Haro Panosyan" <ha**@ti.com> wrote in message
news:de**********@home.itg.ti.com...
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.
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.


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
Aug 19 '05 #6
Victor Bazarov wrote:
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


OK, I am getting clear on 'resize', but still have some doubts about
'reserve'. If I understand correctly, it can be implemented without
using copy-constructors, what is needed just the size of key-element
pair. But then James already talked about this. The question is, if
the above logic(my primitive) is correct, then can 'reserve' be
implemented as part of the standard.
Aug 19 '05 #7
Haro Panosyan wrote:
[...]
OK, I am getting clear on 'resize', but still have some doubts about
'reserve'. If I understand correctly, it can be implemented without
using copy-constructors, what is needed just the size of key-element
pair. But then James already talked about this. The question is, if
the above logic(my primitive) is correct, then can 'reserve' be
implemented as part of the standard.


What does 'reserve' do? The main reason to use 'reserve' for a 'vector'
is to prevent _frequent_ reallocations of the _entire_storage_ and all
the subsequent _copying_ of values (and invalidation of iterators and
references). 'map' organises its storage totally differently. There is
no need to reserve anything because 'map' insertions do not cause any
reallocation of existing storage, any copying, or invalidation. So, our
'reserve' for 'map' would do essentially _nothing_. Yes, we could simply
implement it as

void reserve(size_type) {}

but why?

V
Aug 19 '05 #8
Haro Panosyan <ha**@ti.com> wrote in news:de**********@home.itg.ti.com:
OK, I am getting clear on 'resize', but still have some doubts about
'reserve'. If I understand correctly, it can be implemented without
using copy-constructors, what is needed just the size of key-element
pair. But then James already talked about this. The question is, if
the above logic(my primitive) is correct, then can 'reserve' be
implemented as part of the standard.


Where would it put these "unconstructed" memory blocks?
Aug 19 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: William C. White | last post by:
Does anyone know of a way to use PHP /w Authorize.net AIM without using cURL? Our website is hosted on a shared drive and the webhost company doesn't installed additional software (such as cURL)...
2
by: Albert Ahtenberg | last post by:
Hello, I don't know if it is only me but I was sure that header("Location:url") redirects the browser instantly to URL, or at least stops the execution of the code. But appearantely it continues...
3
by: James | last post by:
Hi, I have a form with 2 fields. 'A' 'B' The user completes one of the fields and the form is submitted. On the results page I want to run a query, but this will change subject to which...
0
by: Ollivier Robert | last post by:
Hello, I'm trying to link PHP with Oracle 9.2.0/OCI8 with gcc 3.2.3 on a Solaris9 system. The link succeeds but everytime I try to run php, I get a SEGV from inside the libcnltsh.so library. ...
1
by: Richard Galli | last post by:
I want viewers to compare state laws on a single subject. Imagine a three-column table with a drop-down box on the top. A viewer selects a state from the list, and that state's text fills the...
4
by: Albert Ahtenberg | last post by:
Hello, I have two questions. 1. When the user presses the back button and returns to a form he filled the form is reseted. How do I leave there the values he inserted? 2. When the...
1
by: inderjit S Gabrie | last post by:
Hi all Here is the scenerio ...is it possibly to do this... i am getting valid course dates output on to a web which i have designed ....all is okay so far , look at the following web url ...
2
by: Jack | last post by:
Hi All, What is the PHP equivilent of Oracle bind variables in a SQL statement, e.g. select x from y where z=:parameter Which in asp/jsp would be followed by some statements to bind a value...
3
by: Sandwick | last post by:
I am trying to change the size of a drawing so they are all 3x3. the script below is what i was trying to use to cut it in half ... I get errors. I can display the normal picture but not the...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.