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);
}
std::dynamic_bitset
... – Randidynamic_bitset
is in Boost. – Varicoloredvector<bool>
, unless you expect it to behave like a standard container. – Sanburnvector<bool>
, then, how should I copy to it the data from a byte array? (I had trouble figuring this out -- doing it bool-by-bool is very slow because of locking issues, so I'm hoping for a bulk operation.) – Valtinbool
, but there are many ways in which it breaks expectations. For one, you cannot take the address of the first element and have a contiguous array of bool, as you can with every other vector. And even though you know the internal representation is an array of bytes, there's no way to access it. P.S. if you ever really need a vector of boolean values that acts like a true vector, usevector<char>
and be aware that it will use 8 times memory of a more optimized solution. – Sanburndynamic_bitset
from Boost seems like a good candidate. But since you don't want Boost, can't you just get that small part from Boost, or copy just that part to a separate library? – Sluggish