c++ 中的位图- unsigned int 能完成这项工作吗?
我不敢承认,我已经有一段时间没有使用 C++ 了,但恐怕我现在必须使用,而且我有点生疏了。
我需要一个位图,而且我确实关心性能,我的位图大小将不再是 25 位。
我正在考虑使用unsigned int,但恐怕我不记得它在c/c++中是如何实现的
unsigned int 是否作为常规二进制数实现?
我也愿意接受任何其他有关位图的建议。
提前感谢您的帮助!
I'm afraid to admit, I haven't used C++ for a while now but i'm afraid I have to now, and I'm a bit rusty.
I need to have a bitmap, and I do care about performance, my bitmap size will be no longer then 25 bits.
I was thinking about using unsigned int, but I am afraid I do not remember how it is implemented in c/c++
Is unsigned int implemented as a regular binary number?
I am also open to any other suggestions for a bitmap.
thanks in advance for the help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用标头
中的std::bitset<25>
。bitset
可以使用[]
进行索引,并且是一个模板,因此它可能会扩展为一个unsigned int
(或等效项),所有操作都内联。Use an
std::bitset<25>
from the header<bitset>
.bitset
can be indexed with[]
and is a template, so it likely expands to just anunsigned int
(or equivalent) with all operations inlined.您是否考虑过
std::bitset > 头文件?示例:
输出:
注意:
bits[0]
是最低有效位,而bits[bits.size()-1]
是最重要的位!在线演示:http://ideone.com/3sSF0
Have you considered std::bitset from
<bitset>
header file?Example:
Output:
Note:
bits[0]
is the least significant bit, whilebits[bits.size()-1]
is the most significant bit!Online demo : http://ideone.com/3sSF0
考虑使用 bitset 代替。
consider a bitset instead.
您应该能够对位图使用整数类型,假设它有足够的位。
然而,标准库中有一个
。You should be able to use an integer type for a bit map, assuming it has enough bits for you.
However, there is a
<bitset>
in the standard library.