By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,652 Members | 1,694 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,652 IT Pros & Developers. It's quick & easy.

char* to vector<bool>

P: n/a
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)

Jul 22 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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.

Jul 22 '05 #2

P: n/a
"Alexandros" <em***@company.com> 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.


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
Jul 22 '05 #3

P: n/a
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

Jul 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.