简体   繁体   English

如何使用std :: vector进行文件I / O <bool> ?

[英]How to do file I/O with std::vector<bool>?

I need to implement a Boolean data container that will store fairly large amount of variables. 我需要实现一个布尔数据容器,该容器将存储大量变量。 I suppose I could just use char* and implement C-style macro accessors but I would prefer to have it wrapped in an std:: structure. 我想我可以只使用char*并实现C样式的宏访问器,但我希望将其包装在std::结构中。 std::bitset<size_t> does not seem practical as it has a fixed-during-compilation size. std::bitset<size_t>似乎不切实际,因为它在编译期间具有固定大小。

So that leaves me with std::vector<bool> which is optimized for space; 这样就留给我std::vector<bool> ,这是为空间优化的; and it has a nice bool-like accessor. 它有一个不错的布尔型访问器。

  1. Is there a way to do something like directly feed a pointer from it to fwrite() ? 有没有办法像直接将指针从fwrite()馈给fwrite()吗?

  2. And how would one do file input into such a vector? 以及如何将文件输入到这样的向量中?

  3. And lastly, is it a good data structure when a lot of file I/O is needed? 最后,当需要大量文件I / O时,它是否是一个好的数据结构?

  4. What about random file access ( fseek etc)? 随机文件访问( fseek等)呢?

EDIT: I've decided to wrap a std::vector<unsigned int> in a new class that has functionality demanded by my requirements. 编辑:我决定将std::vector<unsigned int>包装在具有我的需求所要求的功能的新类中。

  • Is there a way to do something like directly feed a pointer from it to fwrite()? 有没有办法像直接将指针从ffeed()馈给fwrite()一样?

No, but you can do it with std::fstream 不,但是您可以使用std::fstream做到这一点

std::ofstream f("output.file");
std::copy(vb.begin(), vb.end(), std::ostream_iterator<bool>(f, " "));
  • And how would one do file input into such a vector? 以及如何将文件输入到这样的向量中?

Using std::fstream 使用std::fstream

std::ifstream f("input.file");
std::copy(std::istream_iterator<bool>(f), {}, std::back_inserter(vb));
  • And lastly, is it a good data structure when a lot of file I/O is needed? 最后,当需要大量文件I / O时,它是否是一个好的数据结构?

No, vector<bool> is rarely a good data structure for any purpose. 不, vector<bool>很少是用于任何目的的良好数据结构。 See http://howardhinnant.github.io/onvectorbool.html 参见http://howardhinnant.github.io/onvectorbool.html

  • What about random file access (fseek etc)? 随机文件访问(fseek等)呢?

What about it? 那呢

You could use a std::vector<char> , resize it to the size of the file (or other size, say you want to process fixed length blocks), then you can pass the contents of it to a function like fread() or fwrite() in the following way: 您可以使用std::vector<char> ,将其大小调整为文件大小(或其他大小,例如要处理固定长度的块),然后可以将其内容传递给fread()类的函数。或fwrite()以下列方式:

std::vector<char> fileContents;
fileContents.resize(100);
fread(&fileContents[0], 1, 100, theFileStream);

This really just allows you to have a resizable char array, in C++ style. 实际上,这只允许您使用C ++样式调整大小的char数组。 Perhaps it is a useful starting point? 也许这是一个有用的起点? The point being that you can directly access the memory behind the vector, since it is guaranteed to be laid out sequentially, just like an array. 关键是您可以直接访问向量后面的内存,因为可以保证它像数组一样顺序地放置。

The same concept would work for a std::vector<bool> - I'd just be careful when fread ing into this as off the top of my head I can't tell you how big ( sizeof wise) a bool is, as it depends on the platform (8bit vs 16bit vs 32bit, if you were working on a microcontroller for example). 同样的概念将工作一个 std::vector<bool> -当我只是要小心 fread荷兰国际集团到这是从我的头顶,我不能告诉你有多大( sizeof一个明智的) bool是,作为它取决于平台(例如,如果您在微控制器上工作,则为8位,16位和32位)。

It seems std::vector<bool> can be optimised to store each bool in a single bit, so, definitely do not attempt to use the memory behind a vector<bool> directly unless you know it is going to work! 似乎std::vector<bool>可以优化为将每个布尔存储在单个位中,因此,除非您知道它将起作用,否则绝对不要尝试直接使用vector<bool>后面的内存!

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

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