在std :: bitset中更改位范围值的最有效方法 [英] The most efficient method to change the value of a range of bits in std::bitset

查看:276
本文介绍了在std :: bitset中更改位范围值的最有效方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一种低复杂度的有效方法来更改位集中的某个位范围的值:

I need an efficient method with low complexity to change the values of a range of bits in the bitset :

bitset<1000000> bs;

例如,我需要将值设置为100到500到TRUE.

and I need to set values from 100 to 500 to TRUE for example .

以最低的复杂度完成此任务的最快方法是什么?

what is the fastest method to accomplish this in the lowest complexity ?

肯定不能满足要求.

推荐答案

普通的for循环是最简单,最快的方法:

An ordinary for-loop is the easiest and fastest way:

std::bitset<1000000> bs;

for (unsigned i = start, stop = start + n; i != stop; ++i)
  bs.set(i);

不幸的是:

  • given the size of bs, working with masks / shifting isn't an option (e.g. see in bitset, can i use "to_ulong" for a specific range of bits?)
  • the interface of bitset doesn't support ranges / sub-blocks (e.g. What is the performance of std::bitset?). The same limitation is present in boost::dynamic_bitset.

还要考虑std::bitset并不随迭代器,因此它不能与<algorithm>库中的函数一起使用.

Also consider that std::bitset doesn't come with iterators, so it cannot be used with the functions in <algorithm> library.

如果性能确实很重要,则可以考虑临时实现.

If performance really matters you could consider an ad-hoc implementation.

这篇关于在std :: bitset中更改位范围值的最有效方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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