Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6713621
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:24:07+00:00 2026-05-26T08:24:07+00:00

I have a map declared inside a class as follows: class Example { public:

  • 0

I have a map declared inside a class as follows:

class Example {
    public:
        Example()
        {
            std::map< std::string, std::string > map_data;
            map_data["LOCATION"] = "USA";
            map_data["WEBSITE"] = "http://www.google.com/";

            custom_map["nickb"] = map_data;
        }
        std::map< std::string, std::map< std::string, std::string > > get_map() { return custom_map; }
    private:
        std::map< std::string, std::map< std::string, std::string > > custom_map;

        friend class boost::serialization::access;
        template<class Archive>
        void serialize( Archive &ar, const unsigned int version)
        {
            ar & BOOST_SERIALIZATION_NVP( custom_map);
        }
};

And I’d like to be able to serialize just the map it to a variable using boost.

The examples seem to be serializing the entire class, which I do not need to do. They’re also writing to a file, which seems inefficient to me, as I do not need to archive the state of the map to a file, just represent it in a way that can be restored later.

Right now I have this to save the map:

// Create an Example object
Example obj;

// Save the map
std::stringstream outstream( std::stringstream::out | std::stringstream::binary);
boost::archive::text_oarchive oa( outstream);
oa << obj; // <-- BOOST SERIALIZATION STATIC WARNING HERE

// Map saved to this string:
std::string saved_map = outstream.str();

And this to restore it:

// Now retore the map
std::map< std::string, std::map< std::string, std::string > > restored_map;
std::stringstream instream( saved_map, std::stringstream::in | std::stringstream::binary);
boost::archive::text_iarchive ia( instream);
ia >> restored_map;

std::map< std::string, std::string > map_data = restored_map.find( "nickb")->second;
std::cout << "nickb " << map_data["LOCATION"] << " " << map_data["WEBSITE"] << std::endl;

But it is not working. Can anybody give me some tips or show me how to serialize and restore the map?

Thank you.

Edit:
I’ve updated my example with more detail and taken into consideration the answers from K-ballo and Karl Knechtel (Thank you!). This has solved almost all of the errors except one, which is a boost serialization static warning at the above commented line. The warning is:

[Warning] comparison between signed and unsigned integer expressions 

Any idea how to resolve this warning so it compiles? Thanks!

Edit:
My problem was twofold:
I needed to add: BOOST_CLASS_TRACKING( Example, track_never)
And I was serializing the whole class, and trying to unserialize a map.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T08:24:08+00:00Added an answer on May 26, 2026 at 8:24 am

    It looks like you have two completely separate stringstream objects: one of which receives the data, and a second (with no underlying string data) from which you attempt to restore the data. A stringstream isn’t data storage anyway; a string is – in much the same way that an fstream writes to a file on disk which is the actual storage.

    Grab the .str() of the stream that you’re saving to, and use it to initialize the stream that you read from.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a map declared as follows: map < string , list < string
I have a map defined like this std::map<some_key_type, std::string::iterator> mIteratorMap; And a huge string
I have the following code. template<class key,class val> bool has_key(key chkey,std::map<key,val> map){ for (std::map<key,val>::iterator
I have an unmanaged C++ class which has a com map inside of it.
I have a std::map declared thusly in a legacy MFC application: typedef std::map<long, CNutrientInfo>
I have declared the following method: private void mockInvokeDBHandler(Map<String, Object>... rows) { List<Map<String, Object>>
I have a map which I have declared as follows: map<int, bool> index; and
Supposed I have a type I'll call NamedNestedMap std::map<std::string, std::map<std::string, NamedNestedMap> > In this
I have a map declared as follows - Map<Date, Long[]> myMap = new TreeMap<Date,
I have a std::map called 'prompts' which is declared like this: std::map<const int, wstring,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.