在C ++中定义一个大的bitset [英] Define a large bitset in C++

查看:211
本文介绍了在C ++中定义一个大的bitset的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的程序中,我需要检查是否已经生成一组值为2.5 * 10 ^ 9的值。我期望生成大约一半的集合,并需要有一个快速的方式来检查和更新它。 bitset在我看来是一个好主意,因为它不需要太多的内存(每个值1位),速度快。

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

有什么想法吗?

谢谢

PS:如果可能,我宁愿不使用外部库。我已经在使用 GMP ,但我不认为他们有大量的实施。

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.

推荐答案

这可能不是你的问题,但尝试在堆上分配与新的bitset,而不是使用堆栈。

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.

这篇关于在C ++中定义一个大的bitset的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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