char[size] to vector<bool>

I can open a binary file and load it to a char array. Is it possible to convert this char array to a vector<bool> for easy manipulation of the bits?

Any help is appreciated.
Only if you want to mess with allocator classes.

Food for thought:
http://www.informit.com/guides/content.aspx?g=cplusplus&seqNum=98

I would recommend just creating yourself functions that manipulate bits and avoiding vector<bool> just because it is deprecated.

1
2
3
4
5
6
7
8
9
bool get_bit( unsigned char* bits, unsigned bytes, unsigned bit )
  // Takes an array of byte-packed 'bits'
  // of length 'bytes' bytes, and returns the indexed 'bit'.
  {
  unsigned byte_index = bit / 8;
  unsigned bit_index  = bit % 8;
  unsigned mask       = 1 << bit;
  return (bits[ byte_index ] & mask) != 0;
  }

Etc.

Of course, there will still be bitvectors, which is really just a vector <bool>, so if you really want to use it you still can..
Topic archived. No new replies allowed.