在std :: bitset中更改位范围值的最有效方法 [英] The most efficient method to change the value of a range of bits in std::bitset
本文介绍了在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);
不幸的是:
- 鉴于
bs
的大小,不能使用蒙版/移位(例如,请参见以位为单位,我可以使用"to_ulong"以表示特定范围的比特?) -
bitset
的接口不支持范围/子块(例如, std :: bitset的性能如何? ). boost :: dynamic_bitset 中存在相同的限制.
- 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屋!
查看全文