title: How to serialize a (nested) map to a variable using boost?
body: <p>I have a map declared inside a class as follows:</p>
  
  <pre><code>class Example {
      public:
          Example()
          {
              std::map&lt; std::string, std::string &gt; map_data;
              map_data["LOCATION"] = "USA";
              map_data["WEBSITE"] = "http://www.google.com/";
  
              custom_map["nickb"] = map_data;
          }
          std::map&lt; std::string, std::map&lt; std::string, std::string &gt; &gt; get_map() { return custom_map; }
      private:
          std::map&lt; std::string, std::map&lt; std::string, std::string &gt; &gt; custom_map;
  
          friend class boost::serialization::access;
          template&lt;class Archive&gt;
          void serialize( Archive &amp;ar, const unsigned int version)
          {
              ar &amp; BOOST_SERIALIZATION_NVP( custom_map);
          }
  };
  </code></pre>
  
  <p>And I'd like to be able to serialize just the map it to a variable using boost. </p>
  
  <p>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.</p>
  
  <p>Right now I have this to save the map:</p>
  
  <pre><code>// 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 &lt;&lt; obj; // &lt;-- BOOST SERIALIZATION STATIC WARNING HERE
  
  // Map saved to this string:
  std::string saved_map = outstream.str();
  </code></pre>
  
  <p>And this to restore it:</p>
  
  <pre><code>// Now retore the map
  std::map&lt; std::string, std::map&lt; std::string, std::string &gt; &gt; restored_map;
  std::stringstream instream( saved_map, std::stringstream::in | std::stringstream::binary);
  boost::archive::text_iarchive ia( instream);
  ia &gt;&gt; restored_map;
  
  std::map&lt; std::string, std::string &gt; map_data = restored_map.find( "nickb")-&gt;second;
  std::cout &lt;&lt; "nickb " &lt;&lt; map_data["LOCATION"] &lt;&lt; " " &lt;&lt; map_data["WEBSITE"] &lt;&lt; std::endl;
  </code></pre>
  
  <p>But it is not working. Can anybody give me some tips or show me how to serialize and restore the map?</p>
  
  <p>Thank you.</p>
  
  <p><strong>Edit:</strong>
  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:</p>
  
  <pre><code>[Warning] comparison between signed and unsigned integer expressions 
  </code></pre>
  
  <p>Any idea how to resolve this warning so it compiles? Thanks!</p>
  
  <p><strong>Edit:</strong>
  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.</p>