getline如何使用cin? [英] How does getline work with cin?

查看:134
本文介绍了getline如何使用cin?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我觉得有很多类似的问题,所以我真的很抱歉,如果这是一个重复。我找不到这个具体问题的答案。

I feel like there are a lot of similar questions, so I'm really sorry if this is a duplicate. I couldn't find the answer to this specific question, though.

我很困惑,当cin传递给它时getline如何工作,因为我的理解是,应该在每次调用cin时调用cin。当使用在我正在阅读的书中的代码,getline被调用几次,但只有一个输入被发送。除了在这些getline调用中,cin对象不会从任何地方调用。

I am confused as to how getline works when cin is passed to it, because my understanding is that it should be calling cin each time it is called. When working with code that was in a book I'm reading though, getline is called several times yet only one input is sent. The cin object is not called from anywhere except for within these getline calls.

这里发生了什么?当达到getline时,程序简单地在其轨道中停止,并等待输入流传递包括所需分隔符的值?如果是这种情况,后续的getline调用是不是必须等待,因为输入流已经有数据包括它们各自的分隔符?

What's going on here? When getline is reached does the program simply stop in its tracks and wait for the input stream to pass a value including the desired delimiter? If this is the case, do the subsequent getline calls just not have to wait because the input stream already has data including their respective delimiters? I ran a couple tests that would suggest this could be the case.

这是代码: b $ b

    string firstName;
    getline(cin,firstName,',');

    string lastName;
    getline(cin,lastName,',');

    string job;
    getline(cin,job,'\n');

    cout<<firstName<<" "<<lastName<<" is a "<<job<<endl;;

对不起,如果这是一个愚蠢的问题,但我环顾四周,真的找不到答案。感谢您提供任何帮助!

Sorry again if this is a stupid question, but I looked around and genuinely could not find the answer. Thanks in advance for any help that can be provided!

说明:

此代码为控制台输入First,Last,Job \ n输出First Last is a Job

This code outputs "First Last is a Job" for the console input "First,Last,Job\n"

推荐答案

A使用 cin 调用函数实际上不是用户输入的请求(至少不是直接)。它是来自标准输入的字符请求。在正常程序操作(其中标准输入不是从文件或其它源引导)下,标准输入存储在缓冲器中。如果标准输入缓冲区为空,并且 cin 正在请求更多字符,则系统将通过终端请求用户输入。 (即键盘)。终端请求的这个输入通常是线定向的。也就是说,它会等待您按 Enter 键,然后发送要存储在标准输入缓冲区中的所有数据。如果 cin 在输入缓冲区为空之前获取所需的所有字符,那些字符将一直保留到下一个请求。

A call to a function using cin is not actually a request for user input (at least not directly). It is a request for characters from the standard input. In normal program operation (where standard input is not being directed from a file or other source) standard input is stored in a buffer. If the standard input buffer is empty, and cin is requesting more characters, then your system will request input from the user via the terminal. (i.e. the keyboard). This input which the terminal requests is generally line oriented. That is, it waits for you to press the Enter key, then sends all the data to be stored in the standard input buffer. If cin gets all the characters it needs before the input buffer is empty, those characters remain until the next request.

因此,例如,当您进行此调用:

So, for example, when you make this call:

getline(cin,firstName,',');

,输入缓冲区为空,假设用户输入:

and the input buffer is empty, Let's say the user inputs this:



软件开发人员Benjamin,Lindley 输入


首先,以下字符串存储在输入缓冲区中:

First, the following string is stored in the input buffer:

"Benjamin, Lindley, Software Developer\n"

然后 getline 导致Benjamin从输入缓冲区读取(但丢弃逗号)。

Then getline causes "Benjamin," to be read from the input buffer (but discards the comma).

" Lindley, Software Developer\n"

保留在缓冲区中以便用于 cin 的任何操作。

remains in the buffer for any future operations with cin.

这篇关于getline如何使用cin?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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