DaB*_*zUK 7 c++ stringstream setw iomanip
我正在使用std :: stringstream将固定格式字符串解析为值.但是,要解析的最后一个值不是固定长度.
要解析这样的字符串,我可能会这样做:
std::stringstream ss("123ABCDEF1And then the rest of the string");
ss >> std::setw(3) >> nId
>> std::setw(6) >> sLabel
>> std::setw(1) >> bFlag
>> sLeftovers;
Run Code Online (Sandbox Code Playgroud)
但是如何设置宽度以便输出字符串的其余部分?
通过反复试验,我发现这样做有效:
>> std::setw(-1) >> sLeftovers;
Run Code Online (Sandbox Code Playgroud)
但是正确的方法是什么?
请记住,输入运算符>>在空格处停止读取。
使用 egstd::getline获取字符串的其余部分:
std::stringstream ss("123ABCDEF1And then the rest of the string");
ss >> std::setw(3) >> nId
>> std::setw(6) >> sLabel
>> std::setw(1) >> bFlag;
std::getline(ss, sLeftovers);
Run Code Online (Sandbox Code Playgroud)