Is vectorbool the way to go?

Started by
3 comments, last by Winograd 18 years, 8 months ago
Hi, I'm currently writing this simple entropy coder (Golomb-Rice to be exact) in C++. Coder outputs variable length codewords and thus I have to code some sort of bitstream solution. I'm going to go with a simple bit container. For this vector<bool> would be great, altough I don't see a really efficient way to serialize it into a true binary bitstream (ie not just ASCII ones and zeros :) ). So my question is, is there a way to extract the bits from vector<bool> in 8 bit blocks (ie unsigned chars) fast? I think this could be possible because of the way vector<bool> stores the bools (ie one bit per bool). Only that I don't see how this could be done with the given interface. If that can't be done, what would you suggest instead? Writing my own container?
Advertisement
You might check out boost::dynamic_bitset and see if that meets your requirements. I believe it has the things you are asking for.
Thanks, it is almost what I need. It would be perfect for me if it would offer a const iterator for the internal vector<Block> :(

Now I have to copy all the blocks to an external vector before I can write them to a stream. Perhaps this is not a huge performance hit, but still it's completely unnecessary operation. Well, guess I try it out and profile it.

I also looked for the bitmagic lib. Quite a neat little thing, but not a really fit for my needs.
vector<bool> has a few quirks to it.
If you're going to be using them you might want to scroll to the bottom of this Guru of the Week article. Apparently vector<bool> is not actually a container (as defined by the standard library's requirements for containers).
Yes, I was aware of that, but thanks anyway.

SGI's STL documentation mentions that (sort of). It says that the reference to a bit is: "A proxy class that acts as a reference to a single bit; the reason it exists is to allow expressions like V[0] = true. (A proxy class like this is necessary, because the C++ memory model does not include independent addressing of objects smaller than one byte.)"

So taking the address of the proxy class is not probably something one would like to do and taking the address of one single bit is impossible (on many platforms).

This topic is closed to new replies.

Advertisement