I want to ask two C++ bitset questions.
(1) How to create a bitset with a specified length from a function argument? I mean, for example, I have a function
void f(int n)
And inside f, I need to create bitset<n> bs; Is this something doable?
(2) How to copy part of a bitset bs to form a new bitset? For example, given starting index i1 and ending index i2 where i1>=i2, I need to form a new bitset by copying those bits in bs from the least i2th significant bit exclusive to the least i1th significant bit inclusive (just to conform with STL convention).
Thank you.
(1) Not doable with
std::bitset, because the size needs to be a compile-time constant (integral constant expression). You can useboost::dynamic_bitset, or, alternatively,std::vector<bool>orstd::vector<char>(2)
std::bitsetdoes not a special constructor for what you’re looking for. You will need to write an explicit loop. All the other options listed in (1), however, have a constructor taking two iterators.