简体   繁体   English

C ++将dynamic_bitset存储到文件中

[英]C++ Storing a dynamic_bitset into a file

Sort of a follow up to How does one store a vector<bool> or a bitset into a file, but bit-wise? 关于如何将矢量<bool>或bitset存储到文件中的一个跟进的排序,但是按位?

Basically I am writing a bitset as a binary file with the follow code: 基本上我正在写一个bitset作为二进制文件,其代码如下:

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. 我使用动态bitset方法和to_block_range进入临时向量,然后将块打印到文件中。 It works but I am doubling my memory usage when I use an intermediate vector (vector used is the same size of my bitset). 它可以工作,但是当我使用中间向量时,我的内存使用量增加了一倍(使用的向量与我的bitset的大小相同)。 How can I print the bitset to a file without doubling my memory usage? 如何在不增加内存使用量的情况下将bitset打印到文件中?

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. 如果我可以在块中迭代bitset但是看起来,为了防止其他一些问题,动态bitset的作者故意省略了这种功能,那将是很好的。 Should I use a different datastructure? 我应该使用不同的数据结构吗? If it help for context I am using the bitset in a some bloom filter code. 如果它有助于上下文我在一些布隆过滤器代码中使用bitset。

You should do it manually. 你应该手动完成。 Iterate over the bits, pack them into unsigned char s, and stream.put the chars into the file. 迭代这些位,将它们打包成unsigned char ,并将stream.put输入到文件中。

Directly writing the native block_type causes the file format to depend on platform-specific endianness, which is generally undesirable. 直接写入本机block_type会导致文件格式依赖于特定于平台的字节序,这通常是不合需要的。 (And setting block_type to char would harm performance.) (将block_type设置为char会损害性能。)

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. 看看你的另一个问题,我发现这与Nawaz建议的相同,你可能想要回到使用std::vector<bool>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM