在您投票之前,请仔细阅读,它会变得有趣。基本上我想要将类型char
数组转换为std::string
以便使用std::bitset
操作,但是当我尝试在运行时创建bitset
对象时,出现此错误。将一个字符数组转换为std :: string以传入std :: bitset seg fault
扔 '的std :: invalid_argument' 的一个实例后终止叫什么()位集合:: _ M_copy_from_ptr中止(核心转储)
下面的代码
#include <iostream>
#include <cstdlib>
#include <bitset>
int main()
{
char BYTE_4[4] = { 1, 0, 0, 0};
std::string str_BYTE_4 = std::string(BYTE_4);
std::bitset<32> str_BYTE_4_bit(str_BYTE_4);//crash here
std::cout<<"str_BYTE_4_bit. "<<str_BYTE_4_bit<<std::endl;
return 0;
}
我也尝试了一些其他类型的转换std::stringstream
以及char
和std::string
的指针,无论我传递给std::bitset
构造函数I得到相同的错误?
这些只是我注释掉并从上面的代码中删除的片段,以显示我的尝试。
//char* BYTE_4 = new char[4];
//std::stringstream SS;
//std::string str_BYTE_4 = "0101";
//SS << BYTE_4;
//str_BYTE_4 = SS.str();
//for(int index = 0; index < 4; index++)
// str_BYTE_4 += BYTE_4[index];
//std::string *str_BYTE_4 = new std::string[4];
//for(int index = 0; index < 4; index++)
// BYTE_4[index] = rand()%255;
该字符串应该包含字符'“0''和'” 1'',而不是整数'0'和'1'。 –
这是否解决了它? – pandoragami
它的工作原理!谢谢! – pandoragami