When working with Project Euler problems I often need large (> 10**7) bit array’s.
My normal approach is one of:
bool* sieve = new bool[N];
bool sieve[N];
When N = 1,000,000 my program uses 1 MegaByte (8 * 1,000,000 bits).
Is there a more efficient way to use store bit arrays than bool in c++?
Use
std::bitset(ifNis a constant) otherwise usestd::vector<bool>as others have mentioned (but dont forget reading this excellent article by Herb Sutter)EDIT:
Herb Sutter (in that article) mentions that
EDIT 2:
And if you have used Boost you can use
boost::dynamic_bitset(ifNis known at runtime)