can vector<bool> initialized by int

c++

Solution

`std::vector< bool >` cannot do this, but `std::bitset` can. Since the size of the vector is constant in your case, you should be using `std::bitset< 5 >`. Simply initialize it with the nonnegative integer value you want.

Note that `bitset` does not provide (or imitate) the Container interface, but it does provide an `operator []` returning a bit-proxy object, like `vector<bool>`.

Problem

I want to enum the bool representations of `[0,31]` and store it in `tries` for later uses. ``` static const int N = 5; vector<bool> tries(N); for(int i = 0;i < (2<<N); i++){ //can vector<bool> initialized by int? //so I don't have to do bit operation for (int t = 0; t < N; ++t) { tries[t] = i&(1UL<<t); } ... } ```

Original source