Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Static allocation of the stl map size

Posted on 2001-07-03
4
Medium Priority
?
502 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 300 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…

610 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