从std :: vector获取字节< bool> [英] Get bytes from a std::vector<bool>
问题描述
我有类似以下的东西,并在填充它任意数量的位后,我需要得到的字节写出到一个文件。我没有看到一个办法做到这一点,它似乎有用,所以我必须缺少的东西。任何想法?
I have something like the following, and after populating it with a arbitrary number of bits, I need to get the bytes to write out to a file. I don't see a way to do this and it seems useful, so I must be missing something. Any idea's?
std::vector<bool> a;
a.push_back(true);
a.push_back(false);
a.push_back(false);
a.push_back(true);
a.push_back(false);
a.push_back(true);
a.push_back(true);
a.push_back(false);
推荐答案
std :: vector< ; bool>
实际上不包含bools(iebytes),它包含位!这主要是一个缺点,建议您使用 std :: deque< bool>
,它没有这个功能。
std::vector <bool>
does not actually contain bools (i.e.bytes) , it contains bits! This is mostly a missfeature and you are advised to use std::deque <bool>
, which doesn't have this "feature" instead.
如果你想让存储是连续的,使用 std :: vector< char>
。
And if you want the storage to be contiguous, use std::vector <char>
.
这篇关于从std :: vector获取字节< bool>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!