title: C++ Storing a dynamic_bitset into a file
body: <p>Sort of a follow up to <a href="https://stackoverflow.com/questions/4666450/how-does-one-store-a-vectorbool-or-a-bitset-into-a-file-but-bit-wise">How does one store a vector&lt;bool&gt; or a bitset into a file, but bit-wise?</a></p>
  
  <p>Basically I am writing a bitset as a binary file with the follow code:</p>
  
  <pre><code>boost::dynamic_bitset&lt;boost::dynamic_bitset&lt;&gt;::block_type&gt; filter;
  vector&lt;boost::dynamic_bitset&lt;&gt;::block_type&gt; filterBlocks(filter.num_blocks());
  
  //populate vector blocks
  boost::to_block_range(filter, filterBlocks.begin());
  
  ofstream myFile(filterFilePath.c_str(), ios::out | ios::binary);
  
  //write out each block
  for (vector&lt;boost::dynamic_bitset&lt;&gt;::block_type&gt;::iterator it =
          filterBlocks.begin(); it != filterBlocks.end(); ++it)
  {
      //retrieves block and converts it to a char*
      myFile.write(reinterpret_cast&lt;char*&gt;(&amp;*it),
              sizeof(boost::dynamic_bitset&lt;&gt;::block_type));
  }
  myFile.close();
  </code></pre>
  
  <p>I used the method of dynamic bitset and to_block_range into a temporary vector, then printing out the blocks into the file. It works but I am doubling my memory usage when I use an intermediate vector (vector used is the same size of my bitset). How can I print the bitset to a file without doubling my memory usage?</p>
  
  <p>It would be nice if I could iterate through the bitset in blocks but it seems, to prevent some other problems, the authors of the dynamic bitset intentionally omitted this sort of functionality. Should I use a different datastructure? If it help for context I am using the bitset in a some bloom filter code.</p>