Define a large bitset in C++

2019-01-26 08:07发布

In my program I need to check if I have already generated a value in a set of 2.5*10^9. I expect to generate about the half of the set and need to have a fast way to check and update it. The bitset seemed to me as a good idea as it takes not too much memory (1 bit per value) and is fast.

The problem is that when I define my set in my class, I got a segmentation fault as the size is too big (it works with smaller sizes).

private:
  std::bitset<2500000000UL> cover; // not working
  std::bitset<25000UL> cover; // working

Any idea ?

Thank you

PS: I'd rather not use external library if possible. I'm already using GMP but I don't think they have a bit set implementation for large numbers.

标签: c++ bitset
2条回答
看我几分像从前
2楼-- · 2019-01-26 08:40

Use std::vector instead of std::bitset for large sizes.

查看更多
我想做一个坏孩纸
3楼-- · 2019-01-26 08:48

This may not be your problem, but try to allocate the bitset on the heap with new, instead of using the stack.

Some systems limit the size of the stack, which might be what causes problems for you.

查看更多
登录 后发表回答