Kon*_*lph 46
有没有像readLines这样的方式,直到CTRL + Z被按下或者什么?
是的,正是这样,使用自由std::getline函数(不是istream同名的方法!):
string line;
while (getline(cin, line)) {
// do something with the line
}
Run Code Online (Sandbox Code Playgroud)
这将从输入读取行(包括空格,但不结束换行符),直到达到输入结束或cin发出错误信号.
小智 6
#include <iostream>
#include <string>
using namespace std;
int main()
string s;
while( getline( cin, s ) ) {
// do something with s
}
}
Run Code Online (Sandbox Code Playgroud)