問題描述
我看了也沒有用,我怕是這么簡單的問題,沒人敢問.
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.
這篇關于一根線上有多個輸入的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!