什么是缓冲区?
临时存储区域称为缓冲区。所有标准输入和输出设备都包含一个输入和输出缓冲区。在标准 C/C++ 中,流是缓冲的,例如在标准输入的情况下,当我们按下键盘上的键时,它不会发送到您的程序,而是由操作系统缓冲直到分配时间到那个程序。
它如何影响编程?
在各种情况下,您可能需要清除不需要的缓冲区,以便在所需容器中获取下一个输入,而不是在前一个变量的缓冲区中。比如C遇到“scanf()”后,需要输入字符数组或字符,C++遇到“cin”语句后,需要输入字符数组或字符。一个字符串,我们需要清除输入缓冲区,否则所需的输入被前一个变量的缓冲区占用,而不是被所需的容器占用。在第一次输入后在输出屏幕上按“Enter”(回车)时,由于前一个变量的缓冲区是新容器的空间(因为我们没有清除它),程序跳过以下输入容器。
输入:
输入:
输入:
输入:
输入:
输入:
// C Code to explain why not
// clearing the input buffer
// causes undesired outputs
#include<stdio.h>
int main()
{
char str[80], ch;
// Scan input from user -
// 52cxydh for example
scanf("%s", str);
// Scan character from user-
// 'a' for example
ch = getchar();
// Printing character array,
// prints “52cxydh”)
printf("%sn", str);
// This does not print
// character 'a'
printf("%c", ch);
return 0;
}
52cxydh a输出:
52cxydh
// C++ Code to explain why
// not clearing the input
// buffer causes undesired
// outputs
#include<iostream>
#include<vector>
using namespace std;
int main()
{
int a;
char ch[80];
// Enter input from user
// - 4 for example
cin >> a;
// Get input from user -
// "52cxydh" for example
cin.getline(ch,80);
// Prints 4
cout << a << endl;
// Printing string : This does
// not print string
cout << ch << endl;
return 0;
}
4 52cxydh输出:
4在上述两个代码中,都没有按需要打印输出。原因是缓冲区被占用。“n”字符将保留在缓冲区中,并作为下一个输入读取。 如何解决? 在 C的情况下: 1. 使用“ while ((getchar()) != 'n'); ” : 输入“while ((getchar()) != 'n');” 读取缓冲区字符直到最后并丢弃它们(包括换行符)并在“scanf()”语句清除输入缓冲区并允许在所需容器中输入之后使用它。
// C Code to explain why adding
// "while ( (getchar()) != 'n');"
// after "scanf()" statement
// flushes the input buffer
#include<stdio.h>
int main()
{
char str[80], ch;
// scan input from user -
// 52cxydh for example
scanf("%s", str);
// flushes the standard input
// (clears the input buffer)
while ((getchar()) != 'n');
// scan character from user -
// 'a' for example
ch = getchar();
// Printing character array,
// prints “52cxydh”)
printf("%sn", str);
// Printing character a: It
// will print 'a' this time
printf("%c", ch);
return 0;
}
52cxydh a输出:
52cxydh a2. 使用“fflush(stdin)”:在“scanf()”语句之后键入“fflush(stdin)”也会清除输入缓冲区,但会避免使用它,并且根据 C 将输入流称为“未定义” ++11 标准。 在 C++的情况下: 1. 使用“ cin.ignore(numeric_limits::max(),'n'); ” :- 输入“cin.ignore(numeric_limits::max(),'n');” 在“cin”语句之后丢弃输入流中的所有内容,包括换行符。
// C++ Code to explain how
// "cin.ignore(numeric_limits
// <streamsize>::max(),'n');"
// discards the input buffer
#include<iostream>
// for <streamsize>
#include<ios>
// for numeric_limits
#include<limits>
using namespace std;
int main()
{
int a;
char str[80];
// Enter input from user
// - 4 for example
cin >> a;
// discards the input buffer
cin.ignore(numeric_limits<streamsize>::max(),'n');
// Get input from user -
// 52cxydh for example
cin.getline(str, 80);
// Prints 4
cout << a << endl;
// Printing string : This
// will print string now
cout << str << endl;
return 0;
}
4 52cxydh输出:
4 52cxydh2. 使用“cin.sync()”:在“cin”语句之后输入“cin.sync()”会丢弃缓冲区中剩余的所有内容。尽管“cin.sync()”在所有实现中都不起作用(根据 C++11 及以上标准)。
// C++ Code to explain how " cin.sync();"
// discards the input buffer
#include<iostream>
#include<ios>
#include<limits>
using namespace std;
int main()
{
int a;
char str[80];
// Enter input from user
// - 4 for example
cin >> a;
// Discards the input buffer
cin.sync();
// Get input from user -
// 52cxydh for example
cin.getline(str, 80);
// Prints 4
cout << a << endl;
// Printing string - this
// will print string now
cout << str << endl;
return 0;
}
4 52cxydh输出:
43. 使用“cin >> ws”:在“cin”语句之后输入“cin>>ws”告诉编译器忽略缓冲区,并丢弃字符串或字符数组实际内容之前的所有空格。
// C++ Code to explain how "cin >> ws"
// discards the input buffer along with
// initial white spaces of string
#include<iostream>
#include<vector>
using namespace std;
int main()
{
int a;
string s;
// Enter input from user -
// 4 for example
cin >> a;
// Discards the input buffer and
// initial white spaces of string
cin >> ws;
// Get input from user -
// 52cxydh for example
getline(cin, s);
// Prints 4 and 52cxydh :
// will execute print a and s
cout << a << endl;
cout << s << endl;
return 0;
}
4 52cxydh输出:
4 52cxydh