The most efficient method to change the value of a

2019-07-23 03:13发布

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

bitset<1000000> bs;

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 ?

using a loop wouldn't suffice for sure .

1条回答
倾城 Initia
2楼-- · 2019-07-23 03:56

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);

Unfortunately:

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.

查看更多
登录 后发表回答