在 C++ 中。我将 bitset 初始化为 -3,例如:
std::bitset<32> mybit(-3);
有没有一种优雅的方式可以将 mybit
转换为 -3
。因为 bitset 对象只有 to_ulong
和 to_string
等方法。
最佳答案
使用 to_ulong
将其转换为 unsigned long
,然后进行普通转换将其转换为 int
。
int mybit_int;
mybit_int = (int)(mybit.to_ulong());
关于c++ - 在 C++ 中将 bitset 转换为 int,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19583720/