cin.ignore和cin.sync之间的区别 [英] The difference between cin.ignore and cin.sync

查看:161
本文介绍了cin.ignore和cin.sync之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

cin.ignore cin.sync 之间有什么区别?

推荐答案

cin.ignore 丢弃字符,直到指定的数字,包括)。

cin.ignore discards characters, up to the number specified, or until the delimiter is reached (if included). If you call it with no arguments, it discards one character from the input buffer.

例如, cin.ignore(80,'\

For example, cin.ignore (80, '\n') would ignore either 80 characters, or as many as it finds until it hits a newline.

cin .sync 丢弃输入缓冲区中的所有未读字符。然而,不能保证在每个实现中这样做。因此,如果你想要一致性, ignore 是更好的选择。

cin.sync discards all unread characters from the input buffer. However, it is not guaranteed to do so in each implementation. Therefore, ignore is a better choice if you want consistency.

cin.sync )只会清除剩下的内容。我可以想到的 sync()不能用忽略 code> system(PAUSE);

cin.sync() would just clear out what's left. The only use I can think of for sync() that can't be done with ignore is a replacement for system ("PAUSE");:

cin.sync(); //discard unread characters (0 if none)
cin.get(); //wait for input

使用 cin.ignore() cin.get(),这可能是一个混合:

With cin.ignore() and cin.get(), this could be a bit of a mixture:

cin.ignore (std::numeric_limits<std::streamsize>::max(),'\n'); //wait for newline
//cin.get()

剩下,只要把忽略似乎就会跳过它。然而,如果没有换行符,则两者都将等待两个输入。放弃任何未读的东西解决了这个问题,但是,这又不一致。

If there was a newline left over, just putting ignore will seem to skip it. However, putting both will wait for two inputs if there is no newline. Discarding anything that's not read solves that problem, but again, isn't consistent.

这篇关于cin.ignore和cin.sync之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆