在 C++ 中存储位数组(无 Boost,只是标准容器)(例如表示卷分配位图)的最佳方式是什么?
我认为 std::vector 是个好主意,但是 显然它是邪恶的并且已被弃用,那么还有更好的选择吗?
另外:
如果我在内存中有一个字节数组,我将如何将它们复制到推荐的容器中?
(我无法解决 vector
的问题。)
What's the best way to store a bit array in C++ (no Boost, just standard containers), representing, for example, a volume allocation bitmap?
I thought std::vector<bool>
was a great idea, but apparently it's Evil and deprecated, so is there a better choice?
Also:
If I have a byte array in memory, how would I go about copying them to the recommended container?
(I'm having trouble figuring this out for vector<bool>
.)
发布评论
评论(6)
对于普通 C++,有 std::bitset。
马特·奥斯汀 (Matt Austern) 一篇关于其使用的好文章。
另外:
如果您的字节数组(位数组?)适合 unsigned long,那么您可以直接将其分配给 std::bitset:
For vanilla C++, there's std::bitset.
Matt Austern has a nice article on its use.
Also:
If your byte array (bit array?) fits into an unsigned long, then you can assign it to a std::bitset directly:
6 年后才将其发布给后代:就像一位评论者所说,我得出的结论是使用
std::vector
作为其完全没问题自己的专业类型。您唯一需要注意的是不要将其视为标准bool
容器,因为它不是。Just posting this 6 years later for posterity: like one of the commenters said, I came to the conclusion that it's perfectly fine to use
std::vector<bool>
as its own specialized type. The only thing you need to be careful of is not to treat it like a standardbool
container, since it isn't.std::bitset 就可以了,只要你的位数组是固定的尺寸。
作为旁注,还有 std::dynamic_bitset,但我不能 100% 确定它已成为标准。
The std::bitset will do, as long as your bit array is of fixed size.
As a side note there's also std::dynamic_bitset, but am not 100% sure it made it into the standard.
一个字符数组,然后用 0x1 掩码将充当位数组。
示例:
当然,这些操作通常相对较慢,但如果内存是一个问题,那么这是一个不错的方法。我为此选择了 char 来减少所需的轮班次数。然而,使用整数可能仍然更快。
a char array and then masking by 0x1 will act as a bit array.
Example:
of course these operations are usually comparatively slow, but if memory is an issue this is a decent way to go. I chose char's for this to reduce the number of shifts needed. However it may still be faster with integers.
顺便说一句,我认为您链接到的网站上提出的一些观点是不正确的。在几乎每台计算机上,一位的大小实际上是一个字节(与字符相同),因为计算机只能寻址一个字节,而不是一个字节内的一位(如果可以的话,那么您将只有当前拥有的寻址空间的八分之一)与字节)
我只会使用一个字节作为你的向量,因为它可以让其他阅读你代码的人更好地了解你的应用程序的内存占用。
现代计算机中的内存非常充足,因此您可以使用更大的整数类型,但实际上您不能小于一个字节。
要将数据从一个容器复制到另一个容器,首先为容器
向量创建一个迭代器::iterator myItr = myVector.begin()
,并使用 while 循环或 for 循环迭代该向量,直到 myItr 到达 myVector.end()。
例如
I think some of the points made on the site you linked to are not correct by the way. On almost every computer the size of a bit is really one byte (same as a character) because computers can only address a byte not a bit within a byte (if you could then you would only have one eighth of the addressing space you currently have with bytes)
I would just use a byte for your vector because it gives other people who read your code a better idea of the memory footprint of your application.
Ram is very plentiful in modern computers so you may be able to use larger integral types but realistically you can't go smaller than a byte.
To copy data from one container to another first create an iterator for the container
vector::iterator myItr = myVector.begin()
and iterate through the vector with a while loop or a for loop until myItr reaches myVector.end().
For example
强大的 C/С++ 位数组库:https://github.com/noporpoise/BitArray
Powerful C/С++ bit array library: https://github.com/noporpoise/BitArray