Solved

Static allocation of the stl map size

Posted on 2001-07-03
4
469 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
  • 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

743 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now