|
Boost Users : |
Subject: Re: [Boost-users] [Serialization] [Serialization of stl mapwithpointer to POD type]
From: Robert Ramey (ramey_at_[hidden])
Date: 2008-12-31 00:54:42
even simpler
ar & (map<char*, int, cmp>)data_;
robert Ramey
Robert Ramey wrote:
> Anytime you're serialization a "const" value its a red flag that
> you might really want to do that. "const" values are
> initialized when the object is created and should never
> subsequently change.
>
> anyway, try:
>
> save ..
> ar << data.count();.
> for (iter = data_.begin(); iter != data_.end(); iter++) {
> std::string key(iter->first);
> ar << key;
> ar << iter->second;
> }
>
> load ...
> _data.clear()
> ar >> count;
> while(0 < count){
> std::string key;
> int i;
> ar >> key;
> ar >> i;
> data_.insert(
> key.cstr(), // of course this won't work - but you get the
> idea i
> );
> }
>
> Robert Ramey
>
> Denis Gabaidulin wrote:
>> I have following class with stl container:
>>
>> class storage : {
>> private:
>> map<const char*, int, cmp> data_;
>>
>> friend class boost::serialization::access;
>>
>> template<class Archive>
>> void save(Archive & ar, const unsigned int version) const
>> {
>> map<string, int> local;
>> constDataIter iter;
>>
>> for (iter = data_.begin(); iter != data_.end(); iter++) {
>> local[iter->first] = iter->second;
>> }
>>
>> ar & local;
>> }
>> }
>>
>> This code works, but it slowly(full copy) and consume too much memory
>> (x2) :-(
>>
>> What is the best serialization strategy with such stl containers or
>> pointer to pod type?
>>
>> I cannot change const char* to string, dont ask me why :-))
>>
>> Thanks you for any advice.
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net