Define a large bitset in C++

后端 未结 3 1156
悲哀的现实
悲哀的现实 2020-12-15 22:53

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 a

相关标签:
3条回答
  • 2020-12-15 23:25

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

    0 讨论(0)
  • 2020-12-15 23:36

    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.

    0 讨论(0)
  • 2020-12-15 23:37

    I think the following solution is better than using new

        std::vector<std::bitset<2500000000UL>> wrapper(1);
        auto & cover = wrapper[0];//To avoide unnecessary indirection by wrapper[0]
    

    To demonstrate

    int main(){
        std::vector<std::bitset<2500000000UL>> wrapper(1);
        auto & cover = wrapper[0];
        cover[0] = 1;
        std::cout << cover[0] << " " << cover[2500000000UL - 1];
    }
    
    0 讨论(0)
提交回复
热议问题