遍历真位的std :: bitset中的有效方法是什么? [英] Efficient way of iterating over true bits in std::bitset?

查看:1953
本文介绍了遍历真位的std :: bitset中的有效方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Ciao的是,

有没有遍历一个(可能是巨大的)的std :: bitset的是线性的中位数被设置为true的方法吗?所以我想$不必检查bitset中的每一个位置p $ pvent。迭代应依次返回被设置为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? So 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依赖。然而,所谓的特殊结构的面包车昂德博阿斯树支持迭代的时间为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, a special structure called a van Emde Boas Tree supports 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.

作为一个有点无耻的自我推销的,我有的实现在我的个人网站 VEB队列树。如果是不恰当的,我在这里做广告,请让我知道,我买了下来。

As a bit of Shameless Self-Promotion, I have an implementation of a vEB-tree on my personal website. If it's inappropriate for me to advertise this here, please let me know and I'll take it down.

这篇关于遍历真位的std :: bitset中的有效方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆