[英]C++ ValueError bitset::_M_copy_from_ptr when string and bitset are correct size
I have a string which should represent a 32 bit integer, so i'm creating a bitset to print the bits:我有一个字符串应该代表一个 32 位 integer,所以我正在创建一个位集来打印位:
std::string str = rocksDBSlice.ToString();
std::cout << str.length() << std::endl;
std::bitset<32> bits(str);
std::cout << bits.to_string() << std::endl;
But at run-time I get:但在运行时我得到:
4
ValueError bitset::_M_copy_from_ptr
If the string length is 4 why does the bitset throw?如果字符串长度为 4,为什么 bitset 会抛出?
Constructor of std::bitset
with string argument expects the string to contain only 0
s and 1
s.带有字符串参数的
std::bitset
的构造函数期望字符串仅包含0
和1
。 Probably your string contains values like a
instead of 1100001
.可能您的字符串包含类似
a
而不是1100001
的值。 So if you want the 32 bits of 4 bytes of your input string to be put into a bitset
, you have to convert the string to a sequence of 32 ones and zeros first.因此,如果要将输入字符串的 4 个字节的 32 位放入
bitset
中,则必须先将字符串转换为 32 个 1 和 0 的序列。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.