是否使用 std::vector<bool>C++ 中的对象可以接受,还是应该使用替代方法?

2021-12-21 00:00:00 boolean vector c++ bitset stl

我正在处理用户定义数量的位(我持有一个三维位数组,因此大小呈三次增加 - 假设不少于 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 有一个特殊的初始化器来将它们存储为位(而不是整个字节,或 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 应该被弃用并随后从标准库中删除,而该功能将在一个不同的名字.

The Standard Library defines a specialization of the vector template for bool. The description of this specialization indicates that the implementation should pack the elements so that every bool 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 that vector<bool> should be deprecated and subsequently removed from the standard library, while the functionality will be reintroduced under a different name.

现在,您可能会看到我想要使用 vector 对象,但在阅读之后,我正在考虑使用其他东西.唯一的问题是我不确定要使用什么.我很好奇为什么他们声明该功能应该重新引入(尽管名称不同).

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 对象是否可以接受(因为它们是 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 没有任何问题,只是它不等同于 vector T 是等效于 bool 的整数类型.这仅体现在性能(CPU 一次只能访问字节,其中 vector 中的每个元素都存储在一个位中)和内存访问(对 vector 的第一个元素的引用)>vector 不等同于任何其他 vector 的数组.

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).

相关文章