Define a large bitset in C++

谁都会走 提交于 2019-12-29 06:48:47

问题


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.


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/5780112/define-a-large-bitset-in-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!