【发布时间】:2019-08-20 01:49:51
【问题描述】:
这是我教科书中关于使用/实现字符串流的示例:
int main() {
istringstream inSS; // Input string stream
string lineString; // Holds line of text
string firstName; // First name
string lastName; // Last name
int userAge = 0; // Age
bool inputDone = false; // Flag to indicate next iteration
// Prompt user for input
cout << "Enter \"firstname lastname age\" on each line" << endl;
cout << "(\"Exit\" as firstname exits)." << endl << endl;
// Grab data as long as "Exit" is not entered
while (!inputDone) {
// Entire line into lineString
getline(cin, lineString);
// Copies to inSS's string buffer
inSS.clear(); // <-- HELLO RIGHT HERE
inSS.str(lineString);
// Now process the line
inSS >> firstName;
// Output parsed values
if (firstName == "Exit") {
cout << " Exiting." << endl;
inputDone = true;
}
else {
inSS >> lastName;
inSS >> userAge;
cout << " First name: " << firstName << endl;
cout << " Last name: " << lastName << endl;
cout << " Age: " << userAge << endl;
cout << endl;
}
}
return 0;
}
我不明白为什么需要inSS.clear();。书中指出:
"inSS.clear(); 中的语句是重置状态所必需的 流,以便后续提取从头开始;这 clear 重置流的状态。”
.clear() 所做的只是“set a new value for the stream's internal error state flags.”,它如何导致下一次提取从头开始?
如果我从上面的示例中删除语句 inSS.clear(),它将不起作用。例如:
输入:
joe shmo 23
alex caruso 21
输出:
First name: joe
Last name: shmo
Age: 23
First name: joe
Last name: shmo
Age: 23
这就是我期望在删除声明 inSS.clear() 时会发生的情况。我的理解显然有缺陷,所以请纠正我:
输入:
joe shmo 23
alex caruso 21
getline(cin, lineString); 从cin 流中提取joe shmo 23 到lineString 并在最后丢弃/n。
inSS.str(lineString); 将字符串流缓冲区初始化为字符串lineString。
inSS >> firstName; 将提取joe
inSS >> lastName; 将提取shmo
inSS >> userAge; 将提取23
将inSS留空并准备处理下一个输入。
如果您不调用 clear(),则流的标志(如 eof)不会是 重置,导致令人惊讶的行为
这是什么行为? 那么,实际发生了什么以及为什么需要 .clear()?
【问题讨论】:
-
将
inSS留空并准备处理下一个输入,还是这样?
标签: c++ stringstream