我想枚举 [0,31]
的 bool 表示并将其存储在 tries
中供以后使用。
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);
}
...
}
最佳答案
std::vector< bool >
不能这样做,但是 std::bitset
能够。由于 vector 的大小在您的情况下是恒定的,因此您应该使用 std::bitset< 5 >
.只需使用您想要的非负整数值对其进行初始化即可。
请注意 bitset
不提供(或模仿)Container 接口(interface),但它确实提供了一个 operator []
返回一个位代理对象,比如 vector<bool>
.
关于c++ - vector<bool> 可以由 int 初始化吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17562175/