1.获取输入的空白字符的方法。
①使用istream的get()成员函数(ostream的put()成员函数一般与get配合使用)
如果你需要来回切换是否忽略空白的话,这种方法才是首选
int count = 0;
char c;
while (cin.get(c)) { // Always reads whitespace chars.
count++;
cerr << count << endl;
}
char c;
while (cin.get(c)) { // Always reads whitespace chars.
count++;
cerr << count << endl;
}
②使用noskipws操作符。
只能忽略空白
int count = 0;
char c;
cin >> noskipws; // Stops all further whitespace skipping
while (cin >> c) { // Reads whitespace chars now.
count++;
cerr << count << endl;
}
char c;
cin >> noskipws; // Stops all further whitespace skipping
while (cin >> c) { // Reads whitespace chars now.
count++;
cerr << count << endl;
}