最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

ubuntu - std::bitset and std::vector<bool> space usage on GCC linux 64-bit - Stack Overflow

programmeradmin0浏览0评论

Do all versions of GCC C++ standard library on 64-bit Ubuntu implement this bitset and vector bool specialization with bit packing?

I have a special bit data structure that I want to ensure is as space efficient as possible (up to padding for alignment). It needs to hold up to 2^20 bits. I'm considering implementing it manually on uint64 words with bit operations.

Do all versions of GCC C++ standard library on 64-bit Ubuntu implement this bitset and vector bool specialization with bit packing?

I have a special bit data structure that I want to ensure is as space efficient as possible (up to padding for alignment). It needs to hold up to 2^20 bits. I'm considering implementing it manually on uint64 words with bit operations.

Share Improve this question edited Feb 2 at 5:57 qwr asked Feb 1 at 17:53 qwrqwr 11k6 gold badges70 silver badges115 bronze badges 2
  • Given that bit packing is the entire point of std::bitset, I'd be extremely surprised if it has ever not been the case. – Nate Eldredge Commented Feb 2 at 5:10
  • @NateEldredge Do you know if GCC will do the same for vector<bool>? – qwr Commented Feb 2 at 5:56
Add a comment  | 

1 Answer 1

Reset to default 0

The standard says:

There is no requirement that the data be stored as a contiguous allocation of bool values. A space-optimized representation of bits is recommended instead.

Although that's only a recommendation, it's natural to use similar representations for both std::vector<bool> and std::bitset.

With std::bitset, you could use static_assert() with sizeof to help ensure the size is minimal; that's more difficult with std::vector<bool>.

发布评论

评论列表(0)

  1. 暂无评论