简体   繁体   English

使用 std::string 声明一个 std::bitset 数组

[英]Declaring an array of std::bitset using std::string

Im currently trying to declare an array of 17 std::bitsets, each 32 bits long.我目前正在尝试声明一个包含 17 个 std::bitsets 的数组,每个 32 位长。 I'm doing it like this:我这样做是这样的:

std::bitset<32> mTestInstruction[17]
{
    std::string("01000000001000000000000000000001"),
    std::string("01000000011000000000000001100011"),
    std::string("01000000101000000000000000000001"),
    std::string("10100000000000000000000000001010"),
    std::string("00000000100000010000000010000010"),
    std::string("00000000110001010010000000000001"),
    std::string("01001000111001010000000000000000"),
    std::string("01000100001000110000000000000011"),
    std::string("01000000001000010000000000000001"),
    std::string("10000000000000000000000000000011"),
    std::string("00000000010000000000000000000001"),
    std::string("00000000111000000000000000000001"),
    std::string("00000000111001110000100000000001"),
    std::string("01000000010000100000000000000001"),
    std::string("01000100001000100000000000000010"),
    std::string("10000000000000000000000000001100"),
    std::string("11100000000000000000000000001000"),
};

And I'm receiving the following error:我收到以下错误:

error: could not convert 'std::__cxx11::basic_string<char>(((const char*)"01000000001000000000000000000001"), std::allocator<char>())' from 'std::__cxx11::string {aka std::__cxx11::basic_string<char>}' to 'std::bitset<32u>'

for each of the strings of bits.对于每个位串。

I don't understand why this is happening because according to the cpp reference a std::string is a valid way of constructing a bitset.我不明白为什么会发生这种情况,因为根据 cpp 参考 std::string 是构造位集的有效方法。 Could anyone point out how to fix this issue please?谁能指出如何解决这个问题?

You need to call the constructor of std::bitset like:您需要调用std::bitset的构造函数,例如:

std::bitset< 32 > mTestInstruction[17]
{
    std::bitset< 32 >( std::string( "01000000001000000000000000000001" ) ),
    std::bitset< 32 >( std::string( "01000000011000000000000001100011" ) ),
    // ...
};

or even shorter:甚至更短:

std::bitset< 32 > mTestInstruction[17]
{
    std::bitset< 32 >( "01000000001000000000000000000001" ),
    std::bitset< 32 >( "01000000011000000000000001100011" ),
    // ...
};

The reason why your code is not working is because std::bitset 's constructor that accepts std::string is marked explicit (see here ).您的代码不起作用的原因是因为std::bitset接受std::string的构造函数被标记为显式(请参见此处)。

From C++14 you could use binary literals like:从 C++14 您可以使用二进制文字,例如:

std::bitset< 32 > mTestInstruction[17]
{
    0b01000000001000000000000000000001,
    0b01000000011000000000000001100011,
    // ...
};

You can do it this way:你可以这样做:

std::bitset<32> mTestInstruction[17]{
        std::bitset<32>{std::string("01000000001000000000000000000001")},
        std::bitset<32>{std::string("01000000011000000000000001100011")},
        std::bitset<32>{std::string("01000000101000000000000000000001")},
        std::bitset<32>{std::string("10100000000000000000000000001010")},
        std::bitset<32>{std::string("00000000100000010000000010000010")},
        std::bitset<32>{std::string("00000000110001010010000000000001")},
        std::bitset<32>{std::string("01001000111001010000000000000000")},
        std::bitset<32>{std::string("01000100001000110000000000000011")},
        std::bitset<32>{std::string("01000000001000010000000000000001")},
        std::bitset<32>{std::string("10000000000000000000000000000011")},
        std::bitset<32>{std::string("00000000010000000000000000000001")},
        std::bitset<32>{std::string("00000000111000000000000000000001")},
        std::bitset<32>{std::string("00000000111001110000100000000001")},
        std::bitset<32>{std::string("01000000010000100000000000000001")},
        std::bitset<32>{std::string("01000100001000100000000000000010")},
        std::bitset<32>{std::string("10000000000000000000000000001100")},
        std::bitset<32>{std::string("11100000000000000000000000001000")},
    };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM