473,657 Members | 2,921 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

map::reserve

I was wondering:
how come the stl map class does not have a reserve function like the
useful vector::reserve function?

I was thinking about implementing my own reserve class, by providing
my own allocator, inherting a new class from map, and add it a reserve
function that will work on my allocator and ask it to reserve memory
for future use.
however, I don't know how to call the map's Allocator instance in
order to reserve the memory. can anyone please help?

thanks,

liran
Jul 22 '05 #1
3 17071
"Liran Shaul" <li*****@gmail. com> wrote in message
news:4c******** *************** ***@posting.goo gle.com...
I was wondering:
how come the stl map class does not have a reserve function like the
useful vector::reserve function?
When a std::vector reallocates its storage, it can be a very costly
operation. A simple call to push_back() may cause every element in the
std::vector to be copied to a newly allocated block of memory. A call to
reserve() can avoid these unnecessary allocations and copy operations.
std::map, on the other hand, never needs to copy all of the
existing/remaining elements simply because a new element was inserted or
removed.
I was thinking about implementing my own reserve class, by providing
my own allocator, inherting a new class from map, and add it a reserve
function that will work on my allocator and ask it to reserve memory
for future use.
however, I don't know how to call the map's Allocator instance in
order to reserve the memory. can anyone please help?


Every container has a "get_alloca tor" member function that returns a copy of
its allocator. You might be able to come up with something that retrieves
the allocator and gives it a "hint" that the map will grow in size (like
"myMap.get_allo cator().reserve (...)"), but the effort may not pay off
performance-wise. If std::map's allocations are a performance bottleneck,
then I could see how that might be a reasonable approach to make your
application faster.

--
David Hilsee
Jul 22 '05 #2
>
Every container has a "get_alloca tor" member function that returns a copy
of
its allocator. You might be able to come up with something that retrieves
the allocator and gives it a "hint" that the map will grow in size (like
"myMap.get_allo cator().reserve (...)"), but the effort may not pay off
performance-wise. If std::map's allocations are a performance bottleneck,
then I could see how that might be a reasonable approach to make your
application faster.


Sounds like a pool allocator might be a better solution to the OP's problem.

http://www.boost.org/libs/pool/doc/index.html

john
Jul 22 '05 #3
"John Harrison" <jo************ *@hotmail.com> wrote in message
news:2t******** *****@uni-berlin.de...

Every container has a "get_alloca tor" member function that returns a copy of
its allocator. You might be able to come up with something that retrieves the allocator and gives it a "hint" that the map will grow in size (like
"myMap.get_allo cator().reserve (...)"), but the effort may not pay off
performance-wise. If std::map's allocations are a performance bottleneck, then I could see how that might be a reasonable approach to make your
application faster.

Sounds like a pool allocator might be a better solution to the OP's

problem.
http://www.boost.org/libs/pool/doc/index.html


I agree. I only mentioned a reserve() member function on an allocator to
indulge the OP's idea.

--
David Hilsee
Jul 22 '05 #4

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

Similar topics

44
8771
by: jmoy | last post by:
I am a C programmer graduating to C++. As an exercise I wrote a program to count the number of times that different words occur in a text file. Though a hash table might have been a better choice, I chose to use std::map. However, the program runs at less than half the speed of an equivalent program that uses ordinary handcoded binary trees. The result is not changed very much if I replace std::string as the key with a simple string class...
9
6561
by: John | last post by:
How do I copy all the elements of a map to a vector efficiently? The vector is empty initially and needs to be populated with the map elements in sorted order. Thanks, --j
8
3521
by: Haro Panosyan | last post by:
Seems there is no reserve() function defined for STL map. If so, could someone please explain why? Same goes with resize(). Thanks, -haro
3
2245
by: slonial | last post by:
Hi, I am using two STL maps as data member for COM server with VC++6.0.These two maps are unrelated in the sense that they are storing different data. first map is of type(6 MB in size) std::map<long,ISumInfo*>
5
16529
by: junw2000 | last post by:
When should I use each of these three containers? What is the difference? For list and vector, it is obvious. How about set and map? Thanks a lot.
4
3012
by: bearophileHUGS | last post by:
I have started doing practice creating C extensions for CPython, so here are two ideas I have had, possibly useless. If you keep adding elements to a CPython dict/set, it periodically rebuilds itself. So maybe dict.reserve(n) and a set.reserve(n) methods may help, reserving enough (empty) memory for about n *distinct* keys the programmer wants to add to the dict/set in a short future. I have seen that the the C API of the dicts doesn't...
5
1574
by: d.avitabile | last post by:
Hi everybody, I have defined a standard map as follows map<int,doublenonzeroEntries; because I wanted to take advantage of the container class. Now I have a function with the following interface myfunction( int * theIntegersInTheMap, double * theDoublesInTheMap);
2
5142
by: nerdrakesh | last post by:
Hi - I am using the map stl and is there a way I could specify the hash table size in the map in the beginning - (similar to vector.reserve(size) ). Is there a way I could specify the entries at the beginning. Is there an implementation-independent hint that I could give to map so that it performs better when I add more entries to it.
16
5808
by: xyz | last post by:
I have to run the simulation of a trace file around (7gb contains 116million entries)... presently i am using vector iterators to check the conditions in my program..... it is taking 2 days to finish whole simulation..... my question are the map iterators are faster than vector iterators..... does it improve the performance.... thanks to all
0
8303
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8821
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8723
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8502
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8602
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6162
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4300
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1941
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.