如何检查vector< bool>实际上是一个位而不是字节的向量? [英] How do I check if vector<bool> is actually a vector of bits and not bytes?
问题描述
我需要存储动态数组。
C ++参考页面bool> 具有以下信息:
I need to store a dynamic array of bits.
The C++ reference page on vector< bool > has the following information:
存储不一定是
bool
值,但库实现可以优化存储,以便每个值存储在一个位。
The storage is not necessarily an array of
bool
values, but the library implementation may optimize storage so that each value is stored in a single bit.
我确保我的程序使用向量< bool>
实际上存储在向量中的位而不是布尔值(字节)?
How do I make sure that my program that uses vector<bool>
does in fact store bits in a vector instead of boolean values (bytes)?
推荐答案
不要试图这样做。相反,使用 boost :: dynamic_bitset
,这清楚地表明你真正想要什么。 向量< bool>
优化实际上创建了一些bug的可能性,例如使用迭代器时(因为它通常返回一个代理对象)。
Don't try to do that. Instead, use boost::dynamic_bitset
which clearly indicates what you actually want. The vector<bool>
optimization actually creates a number of possibilities for bugs, for example when using iterators (because it usually returns a proxy object).
这篇关于如何检查vector< bool>实际上是一个位而不是字节的向量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!