是使用 std::vector吗? C++ 中的对象可以接受,还是我应该使用替代方案?

发布于 2024-11-26 00:50:26 字数 1072 浏览 0 评论 0原文

我正在使用用户定义的位数(我持有一个三维位数组,因此大小呈立方体增加 - 假设不少于 512 位),并且需要单独翻转它们。现在,就在计算机上,我使用 bool 类型,因为内存不是问题。我确实计划将来将代码转移到微控制器,因此处理能力和内存要求可能是一个问题。但现在,我只想要速度。

然后我从 C++ STL 中找到了 std::bitset 对象,但我无法在运行时定义位集的大小。然后我发现 std::vector 有一个特殊的初始化程序将它们存储为位(而不是整个字节,或 4 个字节),但后来发现 维基百科中的此部分

标准库定义了向量模板的专门化 对于布尔值。该专业的描述表明 实现应该打包元素,以便每个 bool 仅使用 一点内存。这被广泛认为是一个错误。 [...] C++ 标准委员会和库工作组普遍认为 vector 应被弃用并随后从标准库中删除,而该功能将在一个不同的名字。

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

所以,我的问题是,使用 vector 对象是否可以接受(因为它们是 STL 的一部分)?它们是 C++ 标准的一部分吗?

如果它们的使用不可接受,是否有可接受的替代解决方案(除了我自己定义一个特殊的容器)?我自己有一些想法,但我只是好奇是否有人有更好的解决方案。另外,我想避免使用大型库(同样,我想最终将此代码移植到微控制器)。

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.

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<bool> has a special initializer to store them as bits (instead of entire bytes, or 4 bytes) but then found this section in Wikipedia:

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.

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

雨轻弹 2024-12-03 00:50:26

<一href="http://books.google.com/books?id=RPnWe6QKnCcC&pg=PT93&lpg=PT93&dq=meyers%20avoid%20vector%20bool&source=bl&ots=injzzXkmZh&sig=BvHAPL78h D4yYHAFLpRbqyWORUM&hl=en&ei=KocoTtC6HOXa0QGYmM3MCg&sa=X&oi=book_result&ct=结果&resnum=1&ved=0CBUQ6AEwAA#v=onepage&q&f=false">在“有效的 STL”第 18 条,Scott Meyers 建议:“避免使用向量。”:

作为一个STL容器,实际上只有两点问题
向量<布尔>。首先,它不是 STL 容器。其次,它没有
持有布尔值。除此之外,没有什么可反对的。

In "Effective STL," Item 18, Scott Meyers recommended: "Avoid using vector< bool >.":

As an STL container, there are really only two things wrong with
vector< bool >. First, it's not an STL container. Second, it doesn't
hold bools. Other than that, there's not much to object to.

你怎么这么可爱啊 2024-12-03 00:50:26

vector 没有任何问题,只是它不等同于 vector,因为 T 是等同于 bool 的整数类型。这仅显示了性能(CPU 一次仅访问字节,其中在 vector 中,每个元素都存储在一位中)和内存访问(对 bool> 的第一个元素的引用) >vector 与任何其他 vector 不同,

它是标准的一部分,不幸的是:请参阅23.3.7部分 。代码> (C++0x FDIS)。

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

It is part of the Standard, unfortunately: see section 23.3.7 (C++0x FDIS).

梦幻的味道 2024-12-03 00:50:26

批评是,vector 是唯一不完全符合标准容器要求的标准容器。这有点令人惊讶。

另一点是,vector 强制每个人进行空间优化(通过存储位),而某些用户可能更喜欢速度优化。

除此之外,主要的偏差是容器无法返回对其成员的引用,因为它不存储任何布尔值。这将使奇怪的标准库算法无法编译vector

如果您可以接受这一点,并且它满足对其他一切的需求,那么使用它是完全可以的。

The critique is that vector<bool> is the only standard container that doesn't fully conform to the standard's container requirements. That's a bit of a surprise.

Another point is that vector<bool> forces a space optimization on everyone (by storing bits), when perhaps some users would have preferred a speed optimization.

Other than that, the major deviation is that the container cannot return a reference to its members, because it doesn't store any bools. That will make the odd standard library algorithm fail to compile for vector<bool>.

If you can live with that, and it fits your needs for everything else, it is quite ok to use it.

多情出卖 2024-12-03 00:50:26

boost.dynamic_bitset 与 std::bitset 几乎相同,除了它的大小是在运行时给定的。如果您对 boost 依赖项不感兴趣,它的源代码(完全包含在其头文件中)至少可以提供有关如何编写此类容器的更多想法。

There is boost.dynamic_bitset which is nearly identical to std::bitset, except that its size is given at runtime. If you're not interested in boost dependency, its source code (which is entirely contained in its header file) could at least give more ideas on how such container could be written.

牵你的手,一向走下去 2024-12-03 00:50:26

正确使用 vector 没有任何问题,就像 auto_ptr 没有任何问题一样 - 只要您在继续之前知道其缺点和惊喜即可。

There's nothing wrong with correct usage of vector<bool>, just like there's nothing wrong with auto_ptr- as long as you know the drawbacks and the surprises before going on.

念﹏祤嫣 2024-12-03 00:50:26

另一种选择可能是 BitMagic 虽然我不确定它是否适用于 x86 以外的任何其他架构(它是使用 SIMD 进行了大量优化)。

An alternative could be BitMagic although I'm not sure it works on any other architecture than x86(it's heavily optimized using SIMD).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文