3
문자열 스트림을 사용할 때 내 cpp 프로그램이 범위 지정과 관련하여 이상한 작업을하고 있습니다. 같은 블록에 문자열과 문자열 스트림의 초기화를 사용할 때 문제가 없습니다. 하지만 하나 개의 블록 위에 배치하면 문자열 스트림 그다지 출력 문자열 적절히cpp의 이상한 범위 지정
올바른 동작, 각각의 토큰은 공백으로 분리 프로그램 인쇄 :
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main() {
while (true){
//SAME BLOCK
stringstream line;
string commentOrLine;
string almostToken;
getline(cin,commentOrLine);
if (!cin.good()) {
break;
}
line << commentOrLine;
do{
line >> almostToken;
cout << almostToken << " ";
} while (line);
cout << endl;
}
return 0;
}
잘못된 동작 만 프로그램 인쇄 제 inputline :
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main() {
//DIFFERENT BLOCK
stringstream line;
string commentOrLine;
string almostToken;
while (true){
getline(cin,commentOrLine);
if (!cin.good()) {
break;
}
line << commentOrLine;
do{
line >> almostToken;
cout << almostToken << " ";
} while (line);
cout << endl;
}
return 0;
}
왜 이런 일이 발생합니까?
플러싱에 문제가 있습니까? – Nick