In the following code I've a uint8_t
vector filled from some binary file which contains information in some_size
number of bytes -
std::ifstream db(path.c_str(), std::ios::binary);
std::vector <uint8_t> buffer(some_size, 0);
db.read(reinterpret_cast<char *>(&buffer.front()), some_size);
db.close();
Now I know from some index say 20, there exists 37 bytes of either 1
or 0
which I need to fill into a bitset
. The bitset looks like std::bitset<37> booleans;
and is initialized to all 0
s initially. Now if initial index is 20, so I need to fill booleans
with 37 boolean values that are present in bytes which will range from buffer[20]
to buffer[56]
.
Apart from looping over the vector and manually setting 1 or 0 on each bit in bitset, is there any other way to convert those bytes to this bitset?
Since you don't want to write a hand-coded loop, you can use
std::for_each
:The underlying issue is that
std::bitset
doesn't have iterators, so the familiar STL algorithm functions such asstd::copy
,std::copy_n
,std::transform
, etc. that require iterators are difficult, if not impossible to be used ifstd::bitset
is a source (or target) in the algorithm function. Thus it seems thatstd::for_each
with a lambda that increments an index will work.