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 8865879
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T16:38:10+00:00 2026-06-14T16:38:10+00:00

Sort of a follow up to How does one store a vector<bool> or a

  • 0

Sort of a follow up to How does one store a vector<bool> or a bitset into a file, but bit-wise?

Basically I am writing a bitset as a binary file with the follow code:

boost::dynamic_bitset<boost::dynamic_bitset<>::block_type> filter;
vector<boost::dynamic_bitset<>::block_type> 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<boost::dynamic_bitset<>::block_type>::iterator it =
        filterBlocks.begin(); it != filterBlocks.end(); ++it)
{
    //retrieves block and converts it to a char*
    myFile.write(reinterpret_cast<char*>(&*it),
            sizeof(boost::dynamic_bitset<>::block_type));
}
myFile.close();

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?

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.

  • 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-06-14T16:38:11+00:00Added an answer on June 14, 2026 at 4:38 pm

    You should do it manually. Iterate over the bits, pack them into unsigned chars, and stream.put the chars into the file.

    Directly writing the native block_type causes the file format to depend on platform-specific endianness, which is generally undesirable. (And setting block_type to char would harm performance.)

    Looking at your other question, I see that this is the same as what Nawaz suggested, and that you might want to go back to using std::vector<bool> instead.

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

Sidebar

Related Questions

My question is sort of a follow on from this question below but I
This question is a (sort of) follow-up to Intercepting/Rerouting TCP SYN packets to C++
This question is sort of a follow-up to my original question here . Let's
...@Sort bit) AS SELECT .............. and I want to Order only if Sort =
As a sort of follow up to the question called Differences between MSIL and
So this question is a sort of follow on from here ( how to
I have one very tricky issue that I am not able to sort out
Say I have a load of strings that follow the same sort of structure
This is sort of a follow-up to this question . If there are multiple
This is a follow up of this excellent question C# Sort and OrderBy comparison

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.