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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T03:31:24+00:00 2026-06-07T03:31:24+00:00

I have a huge vector of boost dynamic_bitset . I want to write the

  • 0

I have a huge vector of boost dynamic_bitset. I want to write the dynamic_bitset vector to a file and later read the file back into a dynamic_bitset vector. Is the memory for dynamic_bitset allocated as a contiguous block of memory (so that I can write the entire vector at once without traversing) ?

The size of the bitset vector is in order of millions. So I am looking for an efficient way to write them to a file instead of iterating through the elements.

I converted the dynamic_bitset to a string and then wrote the string to a file. Later read the string from the file and converted it back to dynamic_bitset.

Below is the code I wrote in C++ using Visual Studio:

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
#include <boost/dynamic_bitset.hpp>
using namespace std;

int main(int argc, char* argv[])
{
  // Initializing a bitset vector to 0s
  boost::dynamic_bitset<> bit_vector(10); 
  bit_vector[0] = 1;   bit_vector[1] = 1;  bit_vector[4] = 1;  bit_vector[7] = 1;  bit_vector[9] = 1;
  cout<<"Input :"<<bit_vector<<endl; //Prints in reverse order

  //Converting dynamic_bitset to a string
  string buffer;
  to_string(bit_vector, buffer);

  //Writing the string to a file
  ofstream out("file", ios::out | ios::binary);  
  char *c_buffer = (char*)buffer.c_str();   
  out.write(c_buffer, strlen(c_buffer));
  out.close();

  //Find length of the string and reading from the file
  int len = strlen(c_buffer);
  char* c_bit_vector = new char(len+1);
  ifstream in;
  in.open("file", ios::binary);
  in.read(c_bit_vector, len);
  c_bit_vector[len] = 0;
  in.close();

  //Converting string back to dynamic_bitset
  string str2 = c_bit_vector;
  boost::dynamic_bitset<> output_bit_vector( str2 );
  cout<<"Output:"<<output_bit_vector<<endl;

  system("PAUSE");
  return 0;
}

But even this method, storing it as a string, takes a long time to write to the file. And when I try to read back from the file into the string, I get an “unhandled access violation exception”. Is there a more efficient way to implement the same?

  • 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-07T03:31:25+00:00Added an answer on June 7, 2026 at 3:31 am

    hm… mb use operator << and operator >>?

    template <typename Ch, typename Tr, typename Block, typename Alloc>
    std::basic_ostream<Ch, Tr>&
    operator<<(std::basic_ostream<Ch, Tr>& os,
               const dynamic_bitset<Block, Alloc>& b)
    
    template <typename Ch, typename Tr, typename Block, typename Alloc>
    std::basic_istream<Ch, Tr>&
    operator>>(std::basic_istream<Ch, Tr>& is, dynamic_bitset<Block, Alloc>& b)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a huge text file and I wanted to write a program which
I have a huge C file (~100k lines) which I need to be able
I have huge number of Word files I need to merge (join) into one
I have huge 1 GB log file. As I know, it shows errors in
I have a huge vector which has a couple of NA values, and I'm
I have huge file (17 million lines) that contains content in this format: AB101XG,57.144165160000000|,-2.114847768000000|;
I have huge amount of data from database, i need to get or store
I have huge data which is static. I need to save it within the
I have huge amount of txt files which contains 64x64 matrices consisting of integers.
I have huge amont of geographic data represented in simple object structure consisting only

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.