cpp中的cin对象
我开始学习cpp并遇到cin作为接收键盘输入的方式。 如果我理解的话, cin 是一个对象并且 >>是为其定义的运算符。 按照它的定义方式,它如何“知道”将单词彼此分开? 还有一件事, 是什么意思: 同时(辛) cin 是 bool 类型吗?如果返回 true 或 false 意味着什么?
I started to learn cpp and encountered cin as a way to receive input from the keyboard.
If I understood, cin is an object and >> is an operator defined for it.
In the way it is defined, how does it "knows" to separate words from each other?
and another thing,
what is the meaning of:
while(cin)
is cin a bool type? what does it mean if it returns true or false?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
调用:
相当于:
就您的其他问题而言,在 C/C++ 中,任何返回
NULL
或零的内容在if
语句中都被视为 false,否则它被视为真实。这就是为什么 if(cin) 行用于检查流中是否有更多数据需要读取的原因。
Calling:
is equivalent to:
As far as your other question goes, in C/C++ anything that returns a
NULL
or zero is treated as false in anif
statement, otherwise it is treated as true.That's why the line:
if(cin)
works to check whether there's more data to be read in the stream.cin 用法
"其中 strm 是标识符istream 对象和变量的对象是支持作为正确参数的任何类型的对象,也可以将一系列提取操作调用为:
这与从同一对象 strm 执行连续提取相同。从
运算符>>
cin usage
"Where strm is the identifier of a istream object and variable is an object of any type supported as right parameter. It is also possible to call a succession of extraction operations as:
which is the same as performing successive extractions from the same object strm" -> from
operator>>
当您使用输入流时,会定义特定的字符来分隔输入中的项目。默认情况下,我相信它是空格字符。因此您可以输入用空格分隔的内容。
When you use the input stream there are specific character(s) defined to separate items in the input. By default I believe it's the space character. So you can enter things separated by spaces.