convert vector<bool> to int

大城市里の小女人 提交于 2019-12-22 10:59:44

问题


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.


回答1:


Here is an example using C++11

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

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.




回答2:


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.



来源:https://stackoverflow.com/questions/24994665/convert-vectorbool-to-int

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!