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.
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
.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With