一根线上有多个输入
我看了也没有用,我怕是这么简单的问题,没人敢问.
I have looked to no avail, and I'm afraid that it might be such a simple question that nobody dares ask it.
一个人可以在一行中从标准输入中输入多个内容吗?我的意思是:
Can one input multiple things from standard input in one line? I mean this:
float a, b;
char c;
// It is safe to assume a, b, c will be in float, float, char form?
cin >> a >> b >> c;
推荐答案
是的,您可以完全使用您描述的语法从 cin
输入多个项目.结果基本上等同于:
Yes, you can input multiple items from cin
, using exactly the syntax you describe. The result is essentially identical to:
cin >> a;
cin >> b;
cin >> c;
这是由于一种称为运算符链接"的技术.
This is due to a technique called "operator chaining".
每次调用 operator>>(istream&, T)
(其中 T
是某种任意类型)都会返回对其第一个参数的引用.所以 cin >>a
返回 cin
,它可以用作 (cin>>a)>>b
等等.
Each call to operator>>(istream&, T)
(where T
is some arbitrary type) returns a reference to its first argument. So cin >> a
returns cin
, which can be used as (cin>>a)>>b
and so forth.
注意,每次调用 operator>>(istream&, T)
首先消耗所有空白字符,然后是满足输入操作所需的尽可能多的字符,最多(但不包括)) 下一个空白字符、无效字符或 EOF.
Note that each call to operator>>(istream&, T)
first consumes all whitespace characters, then as many characters as is required to satisfy the input operation, up to (but not including) the first next whitespace character, invalid character, or EOF.
相关文章