计算std :: vector< bool>中的设置值 [英] Counting set values in std::vector<bool>
问题描述
std :: vector< bool>
是否使用 POPCNT
指令来计算 std :: count
算法的结果?
Does std::vector<bool>
use POPCNT
instruction to compute the result of std::count
algorithm?
如果否,是否可以在不复制粘贴整个向量的情况下实现?为此,我需要访问实际存储在该向量中的 unsigned int
值,有可能吗?
If no, is it possible to implement without copy-pasting the complete vector? To do that, I need to access unsigned int
values actually stored in that vector, is it possible?
如果有问题,我只需要VC ++ 2017解决方案.
If it matters, I only need VC++ 2017 solution.
推荐答案
std :: count
不对 std :: vector< bool>使用
(不幸的是,至少在GNU C ++标准库中). popcnt
;
std::count
doesn't use popcnt
for std::vector<bool>
unfortunately (at least with GNU C++ standard library).
std :: vector< bool>
也不提供对其原始存储的访问, std :: vector< bool> :: data
返回 void
.
std::vector<bool>
doesn't provide access to its raw storage either, std::vector<bool>::data
returns void
.
另一种解决方案是使用 boost :: dynamic_bitset<>
. boost :: dynamic_bitset<> :: count
使用 popcnt
指令,请参见汇编输出.
An alternative solution is to use boost::dynamic_bitset<>
. boost::dynamic_bitset<>::count
uses popcnt
instruction, see assembly output.
GNU C ++ std :: vector< bool>
是次优实现的,其大小为40个字节.而 sizeof(boost :: dynamic_bitset<>)
是32.
GNU C++ std::vector<bool>
is implemented sub-optimally and its size is 40 bytes. Whereas the sizeof(boost::dynamic_bitset<>)
is 32.
这篇关于计算std :: vector< bool>中的设置值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!