How to do it...

To manipulate an std::vector<bool>, use the same methods you would use for an std::vector<T>, as shown in the following examples:

  • Creating an empty vector:
        std::vector<bool> bv; // []
  • Adding bits to the vector:
        bv.push_back(true);  // [1]
bv.push_back(true); // [1, 1]
bv.push_back(false); // [1, 1, 0]
bv.push_back(false); // [1, 1, 0, 0]
bv.push_back(true); // [1, 1, 0, 0, 1]
  • Setting the values of individual bits:
        bv[3] = true;        // [1, 1, 0, 1, 1]
  • Using generic algorithms:
        auto count_of_ones = std::count(bv.cbegin(), bv.cend(), true);
  • Removing bits from the vector:
        bv.erase(bv.begin() + 2); // [1, 1, 1, 1]
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset