title: Faster way to read/write a std::unordered_map from/to a file
body: <p>I am working with some very large <code>std::unordered_map</code>s (hundreds of millions of entries) and need to save and load them to and from a file. The way I am currently doing this is by iterating through the map and reading/writing each key and value pair one at a time:</p>
  
  <pre><code>std::unordered_map&lt;unsigned long long int, char&gt; map;
  
  void save(){
      std::unordered_map&lt;unsigned long long int, char&gt;::iterator iter;
      FILE *f = fopen("map", "wb");
      for(iter=map.begin(); iter!=map.end(); iter++){
          fwrite(&amp;(iter-&gt;first), 8, 1, f);
          fwrite(&amp;(iter-&gt;second), 1, 1, f);
      }
      fclose(f);
  }
  
  void load(){
      FILE *f = fopen("map", "rb");
      unsigned long long int key;
      char val;
      while(fread(&amp;key, 8, 1, f)){
          fread(&amp;val, 1, 1, f);
          map[key] = val;
      }
      fclose(f);
  }
  </code></pre>
  
  <p>But with around 624 million entries, reading the map from a file took 9 minutes. Writing to a file was faster but still took several minutes. Is there a faster way to do this?</p>