Question about vector<bool>

C

continuation.nomad

suppose I have a large byte array which is actually a bit array. I
want to put this into a vector<bool>. What is the most efficient
way? Internally I know a vector<bool> is represented as a byte array,
so I really would just like a function that says "here's the bytes,
just copy the data". It could even do a memcpy for efficiency. Is
this possible? I've been toying around with it a bit, and it seems
like it might not be possible, and that I actually have to loop
through every single item setting bits. I've tried memcpying to &vec
[0] as well, still no luck.
 
Z

Zeppe

suppose I have a large byte array which is actually a bit array. I
want to put this into a vector<bool>. What is the most efficient
way? Internally I know a vector<bool> is represented as a byte array,
so I really would just like a function that says "here's the bytes,
just copy the data". It could even do a memcpy for efficiency. Is
this possible? I've been toying around with it a bit, and it seems
like it might not be possible, and that I actually have to loop
through every single item setting bits. I've tried memcpying to &vec
[0] as well, still no luck.

memcopying to &vec[0] should work if properly done(i.e., if the command
is correct and the vector buffer is correctly allocated), however there
are two correct methods that I would use and that should also be the
fastest ones:

1) construct from iterators
byte foo[128]; // your byte array
std::vector vec(foo, foo+128);

2) std::copy
byte foo[128]; // your byte array
std::vector vec;
vec.reserve(128);
std::copy(foo, foo+128, vec.begin());

Best wishes,

Zeppe
 
J

Juha Nieminen

Zeppe said:
memcopying to &vec[0] should work if properly done

Except that vec[0] doesn't return a reference to the element with a
std::vector<bool>.
 
Z

Zachary Turner

Juha said:
Zeppe said:
memcopying to &vec[0] should work if properly done
  Except that vec[0] doesn't return a reference to the element with a
std::vector<bool>.

smart! That's why we should stick to the rules :p

Best,

Zeppe

After some more experimentation, I found that I could do (&vec[0])-
_Getptr(). However I seriously doubt this method is guaranteed by
the standard, and as such I ultimately decided not to use it and to
roll my own class. What was odd is that is the buffer seems to be a
reverse buffer, so if I copied 100101010 into the buffer, then using
an iterator would give me 010101001, and a revsere_iterator gave me
the correct sequence. Either way, the class seems to limited to be of
much practical use I guess.
 
B

Bo Persson

Zachary said:
Juha said:
Zeppe wrote:
memcopying to &vec[0] should work if properly done
Except that vec[0] doesn't return a reference to the element with
a std::vector<bool>.

smart! That's why we should stick to the rules :p

Best,

Zeppe

After some more experimentation, I found that I could do (&vec[0])-
_Getptr(). However I seriously doubt this method is guaranteed by
the standard, and as such I ultimately decided not to use it and to
roll my own class.

Good idea. Anything starting with underscore uppercase, or containing
double underscores, are implementation details that are subject to
change without notice, and very likely to differ between different
compilers.
What was odd is that is the buffer seems to be a
reverse buffer, so if I copied 100101010 into the buffer, then using
an iterator would give me 010101001, and a revsere_iterator gave me
the correct sequence. Either way, the class seems to limited to be
of much practical use I guess.

The internal representation isn't standardized either. There are
several ways to number the bits in a machine word.


Bo Persson
 

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

No members online now.

Forum statistics

Threads
473,996
Messages
2,570,237
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top