如何在 C++ 中存储位数组?

发布于 2024-12-11 07:34:21 字数 336 浏览 0 评论 0 原文

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

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

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

发布评论

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

评论(6

御弟哥哥 2024-12-18 07:34:21

对于普通 C++,有 std::bitset。

Bitset 与向量(也称为 bit_vector)非常相似:它
包含位的集合,并提供对
每一位。 bitset 和 bitset 之间有两个主要区别
向量。首先,位集的大小不能更改:位集的
模板参数N,指定模板中的位数
位集,必须是整数常量。其次,bitset不是一个Sequence;
事实上,它根本不是一个 STL 容器。

马特·奥斯汀 (Matt Austern) 一篇关于其使用的好文章。

另外
如果您的字节数组(位数组?)适合 unsigned long,那么您可以直接将其分配给 std::bitset:

unsigned long myByteArray = 0xABCD;
std::bitset<32> bitten( myByteArray );

For vanilla C++, there's std::bitset.

Bitset is very similar to vector (also known as bit_vector): it
contains a collection of bits, and provides constant-time access to
each bit. There are two main differences between bitset and
vector. First, the size of a bitset cannot be changed: bitset's
template parameter N, which specifies the number of bits in the
bitset, must be an integer constant. Second, bitset is not a Sequence;
in fact, it is not an STL Container at all.

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:

unsigned long myByteArray = 0xABCD;
std::bitset<32> bitten( myByteArray );
谈下烟灰 2024-12-18 07:34:21

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 standard bool container, since it isn't.

风吹短裙飘 2024-12-18 07:34:21

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.

故事和酒 2024-12-18 07:34:21

一个字符数组,然后用 0x1 掩码将充当位数组。

示例:

char bitarray[4]; // since 4*8 this array actually contains 32 bits

char getBit(int index) {
    return (bitarray[index/8] >> 7-(index & 0x7)) & 0x1;
}

void setBit(int index, int value) {
    bitarray[index/8] = bitarray[index/8] | (value & 0x1) << 7-(index & 0x7);
}

当然,这些操作通常相对较慢,但如果内存是一个问题,那么这是一个不错的方法。我为此选择了 char 来减少所需的轮班次数。然而,使用整数可能仍然更快。

a char array and then masking by 0x1 will act as a bit array.

Example:

char bitarray[4]; // since 4*8 this array actually contains 32 bits

char getBit(int index) {
    return (bitarray[index/8] >> 7-(index & 0x7)) & 0x1;
}

void setBit(int index, int value) {
    bitarray[index/8] = bitarray[index/8] | (value & 0x1) << 7-(index & 0x7);
}

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.

简单气质女生网名 2024-12-18 07:34:21

顺便说一句,我认为您链接到的网站上提出的一些观点是不正确的。在几乎每台计算机上,一位的大小实际上是一个字节(与字符相同),因为计算机只能寻址一个字节,而不是一个字节内的一位(如果可以的话,那么您将只有当前拥有的寻址空间的八分之一)与字节)

我只会使用一个字节作为你的向量,因为它可以让其他阅读你代码的人更好地了解你的应用程序的内存占用。

现代计算机中的内存非常充足,因此您可以使用更大的整数类型,但实际上您不能小于一个字节。

要将数据从一个容器复制到另一个容器,首先为容器

向量创建一个迭代器::iterator myItr = myVector.begin()

,并使用 while 循环或 for 循环迭代该向量,直到 myItr 到达 myVector.end()。

例如

for(vector<bool>::iterator myItr = myVector.begin(); myItr<myVector.end(); ++myItr)
{
   otherContainer.append(*myItr);
}

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

for(vector<bool>::iterator myItr = myVector.begin(); myItr<myVector.end(); ++myItr)
{
   otherContainer.append(*myItr);
}
怎言笑 2024-12-18 07:34:21

强大的 C/С++ 位数组库:https://github.com/noporpoise/BitArray

Powerful C/С++ bit array library: https://github.com/noporpoise/BitArray

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