getline的任务

Task of getline

getline函数可以接受三个参数。首先是对象 cin。第二个是用 class string 定义的对象。第三个是定界符。

  1. 如果没有找到分隔符,会发生什么?

  2. 比如我为delimiter.Then选择了字符't',用户输入了"Jonathan""han" 保留在缓冲区中还是 "han\n" 保留在缓冲区中? (我读到 getline 每次都会删除缓冲区中的换行符,尽管定界符不是换行符,但我不确定这一点。)

这记录在 std::getline

2) Extracts characters from input and appends them to str until one of the following occurs (checked in the order listed)
a) end-of-file condition on input, in which case, getline sets eofbit.
b) the next available input character is delim, as tested by Traits::eq(c, delim), in which case the delimiter character is extracted from input, but is not appended to str.

std::getline(std::cin, str, 't');的情况下,这意味着str将包含Jonat将被提取并丢弃,在输入中保留han\n缓冲区。

#include <iostream>
#include <string>

using namespace std;

int main(void){

string x;
char y;

getline(cin, x, 'n');

cin >> y;

return 0;

}

如果用户输入"John",则“\n”保留在buffer.Finally中,用户不能输入y的字符。但是,用户可以为 y 输入一个字符。换行符去哪儿了?