Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert vector<bool> to int

Tags:

c++

memcpy

I have a vector of bool which I want to copy in a int container of bigger size. Is there a fast way to do this?

To clarify, is there a smarter way to achieve this?

#include <vector>
#include <cstdint>
#include <iostream>
#include <climits>
#include <cassert>


inline size_t bool2size_t(std::vector<bool> in) {
    assert(sizeof(size_t)*CHAR_BIT >= in.size());
    size_t out(0);

    for (size_t vecPos = 0; vecPos < in.size(); vecPos++) {
        if (in[vecPos]) {
            out += 1 << vecPos;
        }
    }

    return out;
} 

int main () {
    std::vector<bool> A(10,0);
    A[2] = A[4] = 1;

    size_t B = bool2size_t(A);

    std::cout << (1 << 2) + (1 << 4) << std::endl;
    std::cout << B << std::endl;
}

I'm looking for something like a memcpy which I can use on a subbyte level.

like image 969
magu_ Avatar asked Dec 02 '22 19:12

magu_


2 Answers

Here is an example using C++11:

#include <vector>
#include <iostream>
#include <numeric>

using namespace std;

int main() {
    vector<bool> b(10,0);
    b[2] = b[4] = 1;
    int i;
    i = accumulate(b.rbegin(), b.rend(), 0, [](int x, int y) { return (x << 1) + y; });
    cout << i << endl;
}

Another solution that uses GCC internals for vector<bool> and is more efficient:

#include <vector>
#include <iostream>
#include <algorithm>

using namespace std;

int main() {
    vector<bool> b(10,0);
    b[2] = 1;
    b[4] = 1;
    auto p = b.begin()._M_p;
    cout << *p << endl;
}

Note though that it is not recommended to use vector<bool> since it is a problematic specialization of vector<T> and has a slightly different API. I recommend using vector<char> instead, or creating your own Bool wrapper class with implicit cast to and from bool.

like image 181
tohava Avatar answered Dec 22 '22 17:12

tohava


The implementation may store the vector<bool> as a bit set the way you want, but it is not required to do so. If you can change the type, look at the bitset template class, which works the same way, but implements e.g. to_ulong.

See this question.

Edit: If Boost is OK, there is dynamic_bitset, which will do the chunking for you if you need to store more than a unsigned long can hold.

like image 34
Krumelur Avatar answered Dec 22 '22 19:12

Krumelur