char* to vector<bool>

A

Alexandros

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)
 
A

Alexandros

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.
 
V

Victor Bazarov

Alexandros said:
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.

Wrap your char array in a class with a special member function to
access those bits:

class MyVectorBool {
int size;
unsigned char *storage;
public:
MyVectorBool(const char* str)
: size(strlen(str))
, storage(new char[size+1])
{
std::copy(str, str + size + 1, storage);
}

// don't forget the other three: d-tor, copy c-tor, assignment op.

bool get_bit(std::size_t n) const
{
unsigned char which_bit = 1 << (n % CHAR_BIT);
return storage[n / CHAR_BIT] & which_bit;
}

void set_bit(std::size_t n, bool towhat)
{
...
}

void clear_bit(std::size_t n)
{
...
}
};

Should be efficient enough...

Victor
 
M

Martijn Lievaart

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top