是否使用 std::vector<bool>;C++ 中的对象可以接受,还是我应该使用替代方法?
我正在使用用户定义的位数(我持有一个 3 维位数组,因此大小按立方增加 - 假设不少于 512 位),并且需要分别翻转它们.现在,只是在计算机上,我使用 bool
类型,因为内存不是问题.我确实计划将来将代码移至微控制器,因此处理能力和内存要求可能是一个问题.不过现在,我只想要速度.
I'm working with a user-defined quantity of bits (I'm holding a three-dimensional array of bits, so the size increases cubically - assume no less then 512 bits), and need to flip them each individually. Right now, just on a computer, I'm using the bool
type, since memory isn't an issue. I do plan to move the code to a microcontroller in the future, and so processing power and memory requirements may be an issue. Right now though, I just want speed.
然后我从 C++ STL中找到了 std::bitset
对象a>,但我无法在运行时定义位集的大小.然后我发现 std::vector<byte>
有一个特殊的初始化程序将它们存储为位(而不是整个字节或 4 个字节),但后来发现 此部分在维基百科中:
I then found the std::bitset
object from the C++ STL, but I can't define a bitset's size at runtime. I then found that the std::vector<byte>
has a special initializer to store them as bits (instead of entire bytes, or 4 bytes) but then found this section in Wikipedia:
标准库定义了 vector
模板的特化对于 bool
.该专业的描述表明实现应该打包元素,以便每个 bool
只使用一点的内存.这被广泛认为是一个错误.[...] C++ 标准委员会和库工作组普遍认为 vector<bool>
应该被弃用并随后从标准库中删除,而该功能将重新引入一个不同的名字.
The Standard Library defines a specialization of the
vector
template forbool
. The description of this specialization indicates that the implementation should pack the elements so that everybool
only uses one bit of memory. This is widely considered a mistake. [...] There is a general consensus among the C++ Standard Committee and the Library Working Group thatvector<bool>
should be deprecated and subsequently removed from the standard library, while the functionality will be reintroduced under a different name.
现在,您可能会看到我想要使用 vector<bool>
对象,但在阅读完之后,我正在考虑使用其他东西.唯一的问题是我不确定要使用 什么.我很好奇为什么他们说应该重新引入功能(尽管使用不同的名称).
Now, you can probably see my want for using a vector<bool>
object, but after reading that, I'm considering using something else. The only problem is that I'm not sure what to use. I was curious though why they state that the functionality should be re-introduced (albeit under a different name).
所以,我的问题是,vector<bool>
对象的使用是否可以接受(因为它们是 STL 的一部分)?它们是 C++ 标准的一部分吗?
So, my question is, would the use of vector<bool>
objects be acceptable (being that they are a part of the STL)? Are they a part of the C++ standard?
如果它们的使用不可接受,是否有可接受的替代解决方案(在我之外自己定义一个特殊容器)?我自己有一些想法,但我只是好奇是否有人有更好的解决方案.另外,我想避免使用大型库(同样,我希望最终将此代码移植到微控制器).
If their use is not acceptable, is there an acceptable, alternative solution (outside me defining a special container myself)? I have a few ideas myself, but I'm just curious if anyone has a better solution. Also, I would like to avoid using large libraries (again, I want to eventually port this code to a microcontroller).
推荐答案
vector<bool>
没有什么问题,只是它不等价于vector<T>
were T 是等价于 bool 的整数类型.这仅显示在性能(CPU 一次仅访问字节,其中在 vector
中,每个元素都存储在一个位中)和内存访问(对 的第一个元素的引用>vector<bool>
不等同于任何其他 vector<T>
的数组.
There's nothing wrong with vector<bool>
, except that it isn't equivalent to a vector<T>
were T is the integer type equivalent to bool. This only shows in performance (CPUs only access bytes at a time, where in a vector<bool>
every element is stored in one bit) and memory access (a reference to a first element of a vector<bool>
is not equivalent to an array like with any other vector<T>
.
不幸的是,它是标准的一部分:参见 23.3.7
(C++0x FDIS) 部分.
It is part of the Standard, unfortunately: see section 23.3.7
(C++0x FDIS).
相关文章