简体   繁体   English

为什么std :: cin不从头开始读取

[英]why is std::cin not reading from the start

I was looking at a question involving how the std::cin works. 我在看一个涉及std::cin如何工作的问题。 This was the question: 这是一个问题:

What does the following program do if, when it asks you for input, you type two names(for example Samuel Beckett)? 如果在要求您输入时键入以下两个名称(例如Samuel Beckett),以下程序会做什么?

#include <iostream>
#include <string>

int main() {

    std::cout << "What is your name? ";
    std::string name;
    std::cin >> name;
    std::cout << "Hello, " << name << std::endl << "And what is yours? ";

    std::cin >> name;
    std::cout << "Hello, " << name << std::endl << "; nice to meet you too!" << std::endl;

    return 0;
}

I expected the output to be : 我期望输出为:

What is your name? Samuel Beckett
Hello, Samuel
And what is yours? Hello, Samuel
; nice to meet you too!

But the output is: 但是输出是:

What is your name? Samuel Beckett
Hello, Samuel
And what is yours? Hello, Beckett
; nice to meet you too!

Can anybody please help, how std::cin is working ? 任何人都可以帮忙, std::cin如何工作?

I know that if i do this: 我知道如果我这样做:

    std::string a, b;
    std::cin >> a >> b;

and type two words in, then a will have the first word and b will have the second.But why is this happening? 并输入两个单词,那么a将有第一个单词, b将有第二个单词,但是为什么会这样呢? Isn't std::cin supposed to initially discard all whitespace first and then read characters till another whitespace is reached??? 不是std::cin应该首先丢弃所有空白,然后读取字符直到到达另一个空白????

Any help is deeply appreciated. 任何帮助深表感谢。

Stream extractors ( >> ) are whitespace-delimited - when it reads, it stops when it hits any whitespace. 流提取器( >> )是由空格分隔的-读取时,遇到任何空格时它将停止。 The next time it's used, it begins reading from where it left off. 下次使用时,它将从中断处开始读取。 If you want to only delimit on newlines, you can use cin.getline() instead. 如果只想在换行符上定界,则可以使用cin.getline()代替。

EDIT: Correction from @Pete Becker. 编辑:来自@Pete Becker的更正。

What does the following program do if, when it asks you for input, you type two names (for example Samuel Beckett)? 如果在要求您输入时键入以下两个名称(例如Samuel Beckett),以下程序会做什么?

operator>> stops reading when it encounters whitespace (amongst other conditions). 当遇到空格时(除其他情况外), operator>>停止读取。 So, the first call to >> returns only Samuel , leaving Beckett in the input buffer for the next call to >> to return. 因此,对>>的第一次调用仅返回Samuel ,将Beckett留在输入缓冲区中,以便对>>的下一次调用返回。 Which is exactly what you see happen. 这正是您所看到的情况。

I know that if i do this: 我知道如果我这样做:

 std::string a, b; std::cin >> a >> b; 

and type two words in, then a will have the first word and b will have the second. 并输入两个单词,则a将具有第一个单词,b将具有第二个单词。

There is no difference whatsoever between 两者之间没有任何区别

std::cin >> a >> b;

And

std::cin >> a;
std::cin >> b;

In the first case, operator>> returns an std::istream& reference to the stream being read from, which is what allows multiple operations to be chained together. 在第一种情况下, operator>>返回对要读取的流的std::istream&引用,该引用允许将多个操作链接在一起。 But the same operations can also be done in separate statements, too. 但是,同样的操作也可以在单独的语句中完成。

Isn't std::cin supposed to initially discard all whitespace first and then read characters till another whitespace is reached??? 不是std :: cin应该首先丢弃所有空白,然后读取字符直到到达另一个空白????

Not std::cin itself, but operator>> , but yes, and that is exactly what is happening here. 不是std::cin本身,而是operator>> ,但是是的,这正是这里发生的情况。

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

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