简体   繁体   English

C ++:cin.peek(),cin >> char,cin.get(char)

[英]C++: cin.peek(), cin >> char, cin.get(char)

I've got this code with use of cin.peek() method. 我已经使用cin.peek()方法获得了此代码。 I noticed strange behaviour, when input to program looks like qwertyu$[Enter] everything works fine, but when it looks like qwerty[Enter]$ it works only when I type double dollar sign qwerty[Enter]$$ . 我注意到奇怪的行为,当程序的输入看起来像qwertyu$[Enter]一切正常,但是当它看起来像qwerty[Enter]$ ,仅当我键入双美元符号qwerty[Enter]$$时,它才起作用。 On the other hand when I use cin.get(char) everything works also fine. 另一方面,当我使用cin.get(char)一切正常。

#include <iostream>
#include <cstdlib>

using namespace std;



int main()
{
char ch;
int count = 0;

while ( cin.peek() != '$' )
{
    cin >> ch;         //cin.get(ch);
    count++;
}

cout <<  count << " liter(a/y)\n";

system("pause");
return 0;
}


//Input:
// qwerty$<Enter>  It's ok
//////////////////////////
//qwerty<Enter>
//$                Doesn't work
/////////////////////////////
//qwerty<Enter>
//$$                 works(?)

It's because your program won't get input from the console until the user presses the ENTER key (and then it won't see anything typed on the next line until ENTER is pressed again, and so on). 这是因为在用户按下ENTER键之前,程序不会从控制台获得输入(然后,在再次按下ENTER键之前,它不会在下一行看到任何键入的内容)。 This is normal behavior, there's nothing you can do about it. 这是正常行为,您无能为力。 If you want more control, create a UI. 如果需要更多控制,请创建一个UI。

Honestly I don't think the currently accepted answer is that good. 老实说,我认为当前接受的答案不是那么好。

Hmm looking at it again I think since, operator<< is a formatted input command, and get() a plain binary, the formatted version could be waiting for more input than one character to do some formatting magic. 嗯,我再次看它,因为operator<<是格式化的输入命令,而get()是纯二进制,格式化的版本可能要等待比一个字符更多的输入才能完成格式化魔术。

I presume it is way more complicated than get() if you look what it can do. 我认为,如果您看它能做什么,它比get()要复杂得多。 I think >> will hang until it is absolutely sure it read a char according to all the flags set, and then will return. 我认为>>会挂起,直到完全确定它会根据设置的所有标志读取一个char ,然后再返回。 Hence it can wait for more input than just one character. 因此,它可以等待的输入不仅仅是一个字符。 For example you can specify skipws . 例如,您可以指定skipws

It clearly would need to peek into more than once character of input to get a char from \\t\\t\\t test . 要从\\t\\t\\t test获取一个char ,显然需要查看多个输入char

I think get() is unaffected by such flags and will just extract a character from a string, that is why it is easier for get() to behave in non-blocking fashion. 我认为get()不受这些标志的影响,只会从字符串中提取一个字符,这就是为什么get()以非阻塞方式更容易表现的原因。

The reason why consider the currently accepted answer wrong is because it states that the program will not get any input until [enter] or some other flush-like thing. 之所以认为当前接受的答案是错误的,是因为它指出该程序在[enter]或其他类似刷新的东西之前将不会获得任何输入。 In my opinion this is obviously not the case since get() version works. 在我看来,由于get()版本有效,显然不是这种情况。 Why would it, if it did not get the input? 如果没有输入,为什么会呢?

It probably still can block due to buffering, but I think it far less likely, and it is not the case in your example. 由于缓冲,它仍然可能会阻塞,但是我认为可能性很小,在您的示例中情况并非如此。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM