将vector <bool>转换为二进制</bool>

时间:2014-08-22 12:02:07

标签: c++

我有vector<bool>,其中包含10个元素。如何将其转换为二进制类型;

vector<bool> a={0,1,1,1,1,0,1,1,1,0}

我想获得二进制值,如下所示:

long long int x = convert2bin(s)
cout << "x = " << x << endl

x = 0b0111101110

注意:矢量大小将在运行时更改,最大大小= 400。

0b很重要,我想使用gcc扩展名或某些文字类型。

7 个答案:

答案 0 :(得分:1)

std::vector<bool> a = { 0, 1, 1, 1, 1, 0, 1, 1, 1, 0 };
std::string s = "";
for (bool b : a)
{
    s += std::to_string(b);
}
int result = std::stoi(s);

答案 1 :(得分:1)

据我了解评论

  

是的它甚至可以容纳400个值

有问题

  

0b很重要

您需要string,而不是int

std::string convert2bin(const std::vector<bool>& v)
{
    std::string out("0b");
    out.reserve(v.size() + 2);
    for (bool b : v)
    {
        out += b ? '1' : '0';
    }  
    return i;
}

答案 2 :(得分:0)

如果你真的想这样做,你就从最后开始。虽然我支持Marius Bancila,但建议使用bitset。

int mValue = 0
for(int i=a.size()-1, pos=0; i>=0; i--, pos++)
    {
        // Here we create the bitmask for this value
        if(a[i] == 1)
        {
            mask = 1;
            mask << pos;
            myValue |= mask;
        }
    }

答案 3 :(得分:0)

您的x只是a的整数表单,因此可以使用std::accumulate,如下所示

long long x = accumulate(a.begin(), a.end(), 0, 
                   [](long long p, long long q) 
                   { return (p << 1) + q; }
                  );

对于400尺寸,您需要std::string

答案 4 :(得分:0)

首先,转换的结果不是文字。因此,您不能将前缀0b应用于变量x。

这是一个例子

#include <iostream>
#include <iomanip>
#include <algorithm>
#include <numeric>
#include <vector>
#include <iterator>
#include <limits>

int main() 
{
    std::vector<bool> v = { 0, 1, 1, 1, 1, 0, 1, 1, 1, 0 };

    typedef std::vector<bool>::size_type size_type;
    size_type n = std::min<size_type>( v.size(), 
                                       std::numeric_limits<long long>::digits + 1 );

    long long x = std::accumulate( v.begin(), std::next( v.begin(), n ), 0ll,
                                   []( long long acc, int value )
                                   {
                                     return acc << 1 | value;
                                   } );

    for ( int i : v ) std::cout << i;
    std::cout << std::endl;

    std::cout << std::hex << x << std::endl;

    return 0;
}

输出

0111101110
1ee

答案 5 :(得分:0)

vector<bool>已经是&#34;二进制&#34;类型。

转换为int的位数不能超过int中的可用位数。但是,如果您希望能够以该格式打印,则可以使用facet并在打印imbue()之前将其附加到区域设置,然后vector<bool>。理想情况下,您将&#34;存储&#34;该区域设置一次。

我不知道用int打印带有0b前缀的GNU扩展程序,但您可以使用打印方面来执行此操作。

更简单的方法是创建一个&#34;包装器&#34;为你的vector<bool>并打印出来。

虽然vector<bool>始终在内部实现为&#34; bitset&#34;没有公开的方法来提取原始数据,也不一定是它的标准表示。

当然,您可以通过迭代将其转换为其他类型,但我猜您可能一直在寻找其他类型的东西?

答案 6 :(得分:0)

如果位数是预先知道的,并且由于某种原因您需要从 std::array 开始而不是直接从 std::bitset 开始,请考虑此选项 (inspired by this book):

#include <sstream>
#include <iostream>
#include <bitset>
#include <array>
#include <iterator>

/**
 * @brief Converts an array of bools to a bitset
 * @tparam nBits the size of the array
 * @param bits the array of bools
 * @return a bitset with size nBits
 * @see https://www.linuxtopia.org/online_books/programming_books/c++_practical_programming/c++_practical_programming_192.html
 */    
template <size_t nBits>
std::bitset<nBits> BitsToBitset(const std::array<bool, nBits> bits)
{
    std::ostringstream oss;
    std::copy(std::begin(bits), std::end(bits), std::ostream_iterator<bool>(oss, ""));
    return std::bitset<nBits>(oss.str());
}

int main()
{
    std::array<bool, 10> a = { 0, 1, 1, 1, 1, 0, 1, 1, 1, 0 };
    unsigned long int x = BitsToBitset(a).to_ulong();
    std::cout << x << std::endl;
    return x;
}