C++ 示例:无法理解正在做什么
C++ Example : Not able to understand what is being done?
我在 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
。然后可能发生两件事:
如果这以某种方式失败(也许用户输入字符而不是整数(,则将设置失败位。这意味着
std::cin >> age
的结果将转换为false
的bool
。!
会将其反转为true
.因此,由于第一个和第二个条件都为真,因此while
循环的主体将执行,告诉用户他们输入的值无效,循环将再次迭代。如果输入成功,
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()
函数,这基本上意味着忽略前面的每个换行符。
- "类模板示例<int>;"语句对 C++11 是什么意思?
- 两种模板示例有什么区别?
- 提升图库示例 凯文培根的六度:示例中的 Vertex() 是什么
- 以下示例中给出的构造有什么不幸之处?
- 是什么导致 glTexSubImage1D() - 加载纹理时在此示例中出现GL_INVALID_VALUE错误?
- std::atomic_flag不提供加载或存储操作有什么缺点吗?(自旋锁定示例)
- 模板模板参数:以下示例中应用了什么规则
- 什么是"递归嵌套模板"的实际用法示例?
- Boost FFT示例-编译时出错,这段代码在做什么
- 此示例中的'aliased local shared_ptr'是什么
- 指针与参考示例,在什么情况下更好
- 此示例中指定的此 128 位 AES 密钥的格式是什么
- 概念没有按预期解决歧义。代码示例有什么问题?
- 获取释放内存顺序与顺序一致性不同的实际示例是什么?
- 在Bjarne Stroustrup的C 书中,这个示例有什么问题
- 具有可以引发的移动操作的仅移动类的示例是什么?
- 在Boost库asio示例中,处理程序分配之前的[this,self]意味着什么
- C++14变量模板:它们的用途是什么?任何用法示例
- 在简单示例中在 c++ 中使用多态性有什么好处
- 什么是通过调用_mm_stream_si64x()实现性能提升的示例程序