Search code examples
c++vectorbooleanbitset

Create a dynamic bitset in standard C++?


I feel like I'm just hoping that they can, but can a vector access bitset member functions?

For instance, can I do something like.

vector<bool> myvector;
myvector.count();
myvector.test(1);

If not is there anyway to make a bitset dynamically using the standard C++ libraries?

EDIT:

I want to use certain bitset functions (test, count) and want to use the constructor bitset (unsigned long val).

Basically I want to create a bitset for some arbitrary val then do some operations with test and count. Then I want to deallocate and recreate the bitset with a decremented val. I want to keep doing this until val is less than 8.

However, it doesn't seem like creating a dynamic bitset is possible and using vector which is dynamic, means I can use some of the nice bitset functions.


Solution

  • Sounds like you don't actualy need a dynamic bitset. std::bitset is copy-assignable so you can do:

    const size_t size = sizeof(unsigned long) * CHAR_BIT;
    typedef std::bitset<size> my_bitset;
    
    unsigned long val = 42;
    my_bitset bs(val);
    
    /* do work */
    
    bs = my_bitset(--val); // assign with new, decremented val