Solved

Static allocation of the stl map size

Posted on 2001-07-03
4
487 Views
Last Modified: 2013-12-14
Hi,
I heard that it is possible to allocate the size of the stl map in the static way.(something like reserve(n) in the stl vector).
Do you know how?
Thanks,
Inna
0
Comment
Question by:innab
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 22

Expert Comment

by:nietod
ID: 6248806
No.  That sort of function does not make sense for a map--or for a list for that matter.   For a vector, you can use that function to "preallocate" the storage space for the data.  That is helpfull since the vector stores all the data in a single block.  But a map is (usualy) a tree structure and this each item is stored in its own block, so there would be no advantage to knowing the final size ahead of time.
0
 
LVL 22

Accepted Solution

by:
nietod earned 100 total points
ID: 6248822
That is sort of confussing isn't it.  Let me try that again.

For a vector the data is stored in a single block, then that block is not large enough, an new block needs to be created and the data copied to the new block.  Thius reserve and capacity make sense for that type of container.   Buf for a container like map or list, there is never a need to perform a large allocation and copy like in vector.  The reason is that map and list store each item in their own block, not a single block, so there to concept of having extra room to expand into doesn't make sense.  
0
 
LVL 9

Expert Comment

by:jasonclarke
ID: 6249079
You could come close to what you want by defining a custom allocator and using some sort of memory pool for the map elements.

This is probably not a task to be undertaken lightly, however.
0
 
LVL 22

Expert Comment

by:nietod
ID: 6249185
Its also not likely to cause the type of speed increase that the reserve operation does--nor is that sort of speed increase really really needed, it is a major limitation to addition in vector, map doesn't suffer it.
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

695 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question