问题描述
对于向量< bool>
?
执行按位操作的最佳方法是什么? code> vector< bool> 是一个专用化,每个布尔使用一个位。为了节省内存,我选择了 vector< bool>
。我知道向量< bool>
有一些问题,但对我的需要是适当的。
as i understand, vector<bool>
is a specialisation that uses one bit per boolean. I chose vector<bool>
for memory saving reasons. I know that there are some problems with vector<bool>
but for my needs it's apropriate.
最好的方式对整个这样的向量进行按位操作?
now - what's the most performant way of aplying bitwise operations to whole such vectors?
如果我在一个for循环中做,并读出每个单独的bool并存储回来,我的理解
if i do it in a for loop and readout each single bool and store it back, the way I understand it a lot more operations are performed inside in order to access the actual values.
感谢!
推荐答案
如果位数在编译时是固定的,那么你最好使用
If the number of bits are fixed at compile time, you would be much better off using std::bitset
如果没有,在运行时),则您应该看到并可以使用)
If not, (i.e. number of bits varies at runtime), then you should see and can use boost::dynamic_bitset
)
在这两种方法中,操作。
In both of these, it is extremely easy to do all the bitwise operations.
这篇关于向量< bool>上的逐位运算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!