On Sat, 27 Dec 2003 12:13:15 +0000, Alexandros wrote:
Alexandros escribió: Hi. How can I create a vector<bool> efficiently from a char* or a
vector<char> ?
For example, if char* c[2] == (8,10) I want vector<bool> v to be:
(0000100000001010)
It would be enough to be able to get access to each bit of the byte
vector, without creating a vector<bool>. I'd like the most efficient
solution.
Efficient in what? Memory? Lookup time? Programmer time? These are
almost orthogonal quantities for this problem.
And are you sure you need the most efficient solution? On todays hardware
a few cycles and/or bytes are hardly noticed, if at all.
I personally would use a std::bitset if the size is known beforehand. If
not, I might have a look at vector<bool>, but as that has many
limitations, I would also look if a vector<char> (wrapped in a suitable
class) would be acceptable. Also a set<bool> might be acceptable, if only
a few bits are set it might even be very efficient.
Back to your original problem. A vector<bool> has the problem that it does
not resize on access through [], so either resize it on every setting of a
bit, or find the maximum value in c and resize to that maximum. From there
it is a simple loop (or for_each with a suitable functor) to set each bit.
HTH,
M4