469,938 Members | 2,481 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,938 developers. It's quick & easy.

saving a std::map instance into harddisk

Hi,
I have a std::map instance that I want to save into harddisk so that I
donot have to recompute it each time. The map itself is keyed by a
string and the values are pointers to other objects. Is it possible the
whole data structure so that it can be loaded later?
Thanks

Jan 2 '07 #1
4 2912
On Tue, 02 Jan 2007 13:29:51 -0800, ul********@gmail.com wrote:
Hi,
I have a std::map instance that I want to save into harddisk so that I
donot have to recompute it each time. The map itself is keyed by a
string and the values are pointers to other objects. Is it possible the
whole data structure so that it can be loaded later?
Yes - but it's by no means trivial. You want to read up on
"serialization":

http://www.parashift.com/c++-faq-lit...alization.html

--
Lionel B
Jan 2 '07 #2
ul********@gmail.com wrote:
I have a std::map instance that I want to save into harddisk so that I
donot have to recompute it each time. The map itself is keyed by a
string and the values are pointers to other objects. Is it possible
the whole data structure so that it can be loaded later?
Not really (or perhaps I don't understand the question). Pointers are
meaningless outside of the current running process, so saving their
values to file would not help. Saving objects behind those pointer
would be better, of course, but then you'd be duplicating them. Maybe
you could rethink your approach or have some kind of permanent "handle"
(number, ID, whatever) assigned to every object (uniquely of course)
and then save those along with the strings...

I don't see a C++ language problem here, though.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jan 2 '07 #3

Rebuilding takes O(nlogn)
Loading takes O(n) with a larger constant.

Even if you could store it on the hard drive, probably it will
take more time to load that map than to rebuild it!

My 2 cents,
--j

Victor Bazarov wrote:
ul********@gmail.com wrote:
I have a std::map instance that I want to save into harddisk so that I
donot have to recompute it each time. The map itself is keyed by a
string and the values are pointers to other objects. Is it possible
the whole data structure so that it can be loaded later?

Not really (or perhaps I don't understand the question). Pointers are
meaningless outside of the current running process, so saving their
values to file would not help. Saving objects behind those pointer
would be better, of course, but then you'd be duplicating them. Maybe
you could rethink your approach or have some kind of permanent "handle"
(number, ID, whatever) assigned to every object (uniquely of course)
and then save those along with the strings...

I don't see a C++ language problem here, though.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jan 2 '07 #4
John wrote:
Rebuilding takes O(nlogn)
Loading takes O(n) with a larger constant.

Even if you could store it on the hard drive, probably it will
take more time to load that map than to rebuild it!
AFAICT there is no interface for std::map that provides for
loading/unloading and maintaining/optimizing the internal structures
other than a "hint" iterator i.e.:

iterator insert(iterator hint, const value_type& x)

Seems like no information but the elements and the order needs to be
stored on disk to employ this.

I doubt that this will result in an O(N) insertion or any significant
optimization for that matter but I'm willing to be proved wrong.
Jan 2 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Antti Granqvist | last post: by
8 posts views Thread by Kin Pang | last post: by
5 posts views Thread by cppaddict | last post: by
14 posts views Thread by Flzw | last post: by
1 post views Thread by Saeed Amrollahi | last post: by
2 posts views Thread by Gernot Frisch | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.