VS中cin.sync()函数并不能清空输入缓冲区,而在VC中可以,这是因为调用sync()后可能会清空/恢复原状/什么都不干,具体哪种行为取决于编译器。
The only portable way to 'clear' an input buffer is by reading and discarding characters until a particular character (for example a new line) occurs or a limit is reached. (“清除”输入缓冲区的唯一可移植方法是读取和丢弃字符,直到出现特定字符(例如新行)或达到限制。)
For example:
cin.ignore(std::numeric_limits<int>::max(),'\n');
例子中的函数作用是跳过输入流中n个字符,或在遇到指定的终止字符时提前结束(此时跳过包括终止字符在内的若干字符)。
可以用cin.ignore(4096,'\n'),即保证前一个参数足够大,这样就会跳过回车键前的所有内容。
![](https://img-blog.csdnimg.cn/img_convert/ef4f8cc219d388953798ad9784f9cc65.png)
There is no way using standard C++ facilities to 'clear everything in the stdin buffer till it is empty'.(没有标准的c++工具能清空缓冲区里的所有内容直到它为空。)