C++ 示例:无法理解正在做什么

C++ Example : Not able to understand what is being done?

本文关键字:什么 示例 C++      更新时间:2023-10-16

我在 http://www.parashift.com/c++-faq-lite/istream-and-ignore.html 找到了这个链接

其中显示"如何让 std::cin 跳过无效的输入字符?

Use std::cin.clear() and std::cin.ignore().
#include <iostream>
#include <limits>
int main()
{
  int age = 0;
  while ((std::cout << "How old are you? ")
         && !(std::cin >> age)) {
    std::cout << "That's not a number; ";
    std::cin.clear();
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), 'n');
  }
  std::cout << "You are " << age << " years oldn";
  ...
}
Of course you can also print the error message when the input is out of range. For example, if you wanted the age to be between 1 and 200, you could change the while loop to:
  ...
  while ((std::cout << "How old are you? ")
         && (!(std::cin >> age) || age < 1 || age > 200)) {
    std::cout << "That's not a number between 1 and 200; ";
    std::cin.clear();
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), 'n');
  }
  ...
Here's a sample run:
How old are you? foo
That's not a number between 1 and 200; How old are you? bar
That's not a number between 1 and 200; How old are you? -3
That's not a number between 1 and 200; How old are you? 0
That's not a number between 1 and 200; How old are you? 201
That's not a number between 1 and 200; How old are you? 2
You are 2 years old

我无法理解它是如何做到的> 任何人都可以解释一下吗?

我对以下方面有疑问:

while ((std::cout << "How old are you? ")
             && !(std::cin >> age)) 

如何检查有效条目?我的意思是问"std::cout <<"你多大了?"和"!(std::cin>> age(",返回真或假,哪些正在被 AND 处理?

另一件令人困惑的事情是用法,

std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), 'n');

有什么目的?在谷歌上搜索了这些功能,但我仍然不清楚。特别是std::numeric_limits<std::streamsize>::max()

有人可以帮忙吗?谢谢

流上的插入和提取运算符<<>>将返回对流本身的引用。这就是为什么你可以像这样将插入串在一起的原因:

std::cout << "Hello, " << "World!";

首先std::cout << "Hello, "返回对std::cout的引用,然后你得到了等效的std::cout << "World!"

流也可以转换为 bool .这基本上检查流的状态是否仍然正常。没有什么是失败的。例如,您可以执行以下操作:

if (std::cin) // ...

这将检查std::cin流是否仍处于良好状态。

现在让我们看一下您询问的代码:

while ((std::cout << "How old are you? ")
         && !(std::cin >> age))

插入std::cout不会导致失败。此处包含它的唯一原因是,每次输入到age之前都会发生。另一种办法是将其放在while之前一次,在while体的末尾放置一次。

插入std::cout完成后,将评估!(std::cin >> age)。这将首先让用户提供一个age。然后可能发生两件事:

  1. 如果这以某种方式失败(也许用户输入字符而不是整数(,则将设置失败位。这意味着std::cin >> age的结果将转换为falsebool!会将其反转为 true .因此,由于第一个和第二个条件都为真,因此 while 循环的主体将执行,告诉用户他们输入的值无效,循环将再次迭代。

  2. 如果输入成功,std::cin >> age的结果将被true! 会将其转换为 false 。这意味着 while 循环的主体将不会执行,也不会告诉用户他们输入了错误的值。

现在让我们看看:

std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), 'n');

这仅在上述情况 1 中发生,当输入失败时。如果流进入失败状态,则在清除状态之前,它将不再接受任何插入或提取。这就是clear()所做的。然后,对 ignore 的调用表示提取流中下一个n之前的所有字符并丢弃它们。这将删除流中的无效输入。

while ((std::cout << "How old are you? ")        && !(std::cin >> age)) 

cout 是类 ostream 的全局对象,cin 是类 istream 的全局对象

这些对象与重载运算符一起使用<<(在 ostream 中定义(和>>(在 istream 中定义(来获取输入(通过调用为这些运算符定义的函数(

operator<< 的返回类型为 ostream&operator>> 的返回类型为 istream& 。然后将这些值转换为布尔值。看到这里。

也检查一下cin.ignore()cin.clear()

std::numeric_limits允许您获得可以容纳给定类型的最大数量。

在您的示例中,它传递给ignore()函数,这基本上意味着忽略前面的每个换行符。