简体   繁体   English

为什么这个程序有效?

[英]Why does this program work?

I recently wrote a program that takes inputted char data, tests if it is acceptable (az, # marks the end of the input) and puts it in a stack which then tests to see if it's a palindrome. 我最近编写了一个程序,它接受输入的char数据,测试是否可以接受(az,#标记输入的结尾)并将其放入堆栈,然后测试它是否是回文。 I was expecting to enter it one char by a time, but if I enter a string ended by pound it.. works. 我希望一次输入一个字符串,但是如果我输入一个以磅为单位的字符串,它就可以了。 Here is some of the relevant code: 以下是一些相关代码:

char buffer;
bool pound_test = false;
bool palindrome = false;
bool keep_going = true;
stack<char> stack1, stack2, stack3;
string str = "";

cout << "Please enter a string, then end it with the pound sign. " << endl;

while(pound_test == false) {
    cin >> buffer;

    if((buffer >= 97) && (buffer <= 122))
    {
        stack1.push(buffer);
        stack2.push(buffer);
        str += buffer;
    }

    if((buffer >= 65) && (buffer <= 90)) {
        buffer = buffer + 32;
        stack1.push(buffer);
        stack2.push(buffer);
        str += buffer;
    }

    if(buffer == '#')
        pound_test = true;
}

So, when the user enters one long string, like "racecar#" and presses enter, the program properly puts it into the stack. 因此,当用户输入一个长字符串,如“racecar#”并按下回车键时,程序会正确地将其放入堆栈中。 My question is simply: why? 我的问题很简单:为什么? Wouldn't the data have to be inputted one char at a time for it to work properly, because the cin is in the loop itself, and the loop has to repeat to enter multiple chars into the stack, right? 数据是否必须一次输入一个char以使其正常工作,因为cin本身就在循环中,并且循环必须重复以将多个字符输入堆栈,对吧? Thanks! 谢谢!

Edit: Thanks for the answers/comments everyone! 编辑:感谢大家的回答/评论! I'm really impressed by the quick and kind replies. 快速而善意的回复让我印象深刻。 I'm certainty going to use this site again. 我肯定会再次使用这个网站。

Console input (via the cin std::istream object) in most systems is line buffered. 大多数系统中的控制台输入(通过cin std::istream对象)是行缓冲的。 So when you call cin::operator>> for a single character, the function does not in fact return until you press newline (because the underlying I/O system does not make data available to cin until then). 因此,当您为单个字符调用cin::operator>> ,该函数实际上不会返回,直到您按下换行符(因为基础I / O系统在此之前不会使数据可用于cin )。 Any data entered up-to and including the <newline> will be buffered and subsequent calls to cin::operator>> will be serviced from the buffer until it is exhausted. 任何输入的数据(包括<newline>都将被缓冲,并且后续对cin::operator>>调用将从缓冲区中提供服务,直到它耗尽为止。

In this case cin >> buffer , where buffer is of type char will indeed get a single character, but before that the console buffered an entire line and will use it to satisfy subsequent console input operations. 在这种情况下, cin >> buffer ,其中buffer的类型为char将确实获得单个字符,但在此之前,控制台缓冲整行并将使用它来满足后续的控制台输入操作。

If you step through your code in your debugger the operation may be clearer to you. 如果您在调试器中单步执行代码,则操作可能会更清晰。

“系统”(操作系统,库,无论什么 - 取决于实现)吃了来自输入的数据字符串,但是你的程序用char读取它。

While all the answers about os buffering are true, I think the confusion can be traced to cin 's operator >> (char) ; 虽然关于os缓冲的所有答案都是正确的,但我认为混淆可以追溯到cinoperator >> (char) ; because C++ can overload methods based on their argument types, the char version of operator >> is only assigning one character at a time, even though the whole string is buffered. 因为C ++可以根据它们的参数类型重载方法,所以char operator >>的char版本一次只能分配一个字符,即使整个字符串是缓冲的。 I believe you're thinking that operator >> should try to put the whole string into your character; 我相信你认为operator >>应该尝试将整个字符串放入你的角色; but since it "knows" you're reading one character at a time, it only assigns one character at a time. 但由于它“知道”你一次只读一个字符,所以它一次只能分配一个字符。 I'm not sure if this is specified behavior for cin or not, but that seems to be what's happening. 我不确定这是否是针对cin的指定行为,但这似乎正在发生的事情。

The cin operator reads from the standard input stream (if not configured otherwise). cin运算符从标准输入流中读取(如果没有另外配置)。 The stdin works as follows: you type and when you press Enter , it is sent to stdin and therefore cin reads the whole string up to the moment when you pressed Enter . stdin工作方式如下:键入并按Enter ,它将被发送到stdin ,因此cin读取整个字符串直到您按Enter的那一刻。

If you wish to read char by char, you should use getchar . 如果您希望通过char读取char,则应使用getchar

The way your keyboard input is seen by cin >> buffer; cin >> buffer;看到键盘输入的方式cin >> buffer; is not a property of your program, but of the combination of OS, Shell, C runtime and maybe thousand things I forgot. 不是你的程序的属性,而是OS,Shell,C运行时的组合,也许是我忘记的千件事。

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

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