我有一个奇怪的错误:我想一个ostringstream的内容复制到无符号字符的vecotr: ostringstream到矢量<uint8_t>
vector<uint8_t> buffer;
ostringstream os;
os << num1 << char1 << num2 << char2;
// 1. this will crash
buffer.insert(buffer.end(), os.str().begin(), os.str().end());
// 2. this also crash
copy(os.str().begin(), os.str().end(), back_inserter(buffer);
string str = os.str();
// 4. this work
buffer.insert(buffer.end(), str().begin(), str().end());
// 5. this also works
copy(str().begin(), tr().end(), back_inserter(buffer);
我不明白为什么1间2的碰撞在视觉演播室2010.
有人有一些建议吗?
EDIT
的解决方案是:
vector<uint8_t> buffer;
ostringstream os;
os << num1 << char1 << num2 << char2;
const string& str = os.str();
// 4. this work
buffer.insert(buffer.end(), str().begin(), str().end());
如果你想'unsigned char',为什么代码使用'uint8_t'?他们不一样。 –
它们是相同的,并在 –
下定义,不,它们不一样。 'uint8_t'在没有8位无符号类型的平台上不存在。所有平台上都存在'unsigned char'。 –