Efficient way of iterating over true bits in std::bitset?(迭代std :: bitset中真实位的有效方法?)
问题描述
有没有一种方法可以迭代(可能很大)std::bitset
,它在 设置为 true 的位数中 线性/em>?我想避免检查位集中的每个位置.迭代应该连续返回每个设置为 true 的位的索引.
Is there a way of iterating over a (possibly huge) std::bitset
that is linear in the number of bits that are set to true? I want to prevent having to check every single position in the bitset. The iteration should successively return the indices of each bit that is set to true.
推荐答案
标准位向量不支持对真实位的有效迭代 - 运行时间总是 O(n),其中 n 是总位数,它没有对 k 的依赖.但是,也有专门的数据结构,例如 van Emde Boas 树 和 y-fast 尝试,支持位迭代在时间 O(k lg lg n) 中,n 是位数,k 是真实位数.
A standard bitvector does not support efficient iteration over true bits - the runtime is always O(n), where n is the number of total bits, which has no dependence on k. However, there are specialized data structures like van Emde Boas trees and y-fast tries, that support iteration over the bits in time O(k lg lg n), where n is the number of bits and k is the number of true bits.
这篇关于迭代std :: bitset中真实位的有效方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:迭代std :: bitset中真实位的有效方法?


基础教程推荐
- 向量<unique_ptr<A>>使用初始化列表 2022-10-23
- C语言3个整数的数组 1970-01-01
- C++多态 1970-01-01
- 明确指定任何或所有枚举数的整数值 1970-01-01
- C++:为什么结构类需要一个虚拟方法才能成为多态? 2022-10-19
- 总计将在节日礼物上花多少钱 1970-01-01
- 对 STL 容器的安全并行只读访问 2022-10-25
- 用指数格式表示浮点数 1970-01-01
- 迭代std :: bitset中真实位的有效方法? 2022-10-18
- C语言数组 1970-01-01