I had that same problem recently, however just like a std::array you need to know the size of the bitset at compile-time, since it's a template parameter. I found boost::dynamic_bitset as an alternative, and it worked like a charm.
c++ vector of bitset pass to a function
Pergunta
I want to implement an algorithm in C++ that needs a dynamically assigned huge vector of bitset (512x18,000,000 bits - I have 16Gb of RAM).
a) This works fine
int nofBits=....(function read from db);
vector < bitset <nofBits> > flags;
flags.resize(512);
but how do I pass it (by reference) to a function? Keep in mind, I do not know nofBits in compile time.
I could use a
vector<vector<bool> >
but would not it be worse in terms of memory usage?
Solução
Outras dicas
std::vector<bool>
is specialised to use memory efficiently. It is roughly as space efficient as std::bitset<N>
(a few extra bytes because its size is dynamic and the bits live on the heap).
Note, however, that std::vector<bool>
has issues, so tread lightly.
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow