Multiple inputs on one line(一根线上有多个输入)
问题描述
我看了也没有用,我怕是这么简单的问题,没人敢问.
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.
这篇关于一根线上有多个输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:一根线上有多个输入


基础教程推荐
- 我有静态或动态 boost 库吗? 2021-01-01
- 常量变量在标题中不起作用 2021-01-01
- 如何检查GTK+3.0中的小部件类型? 2022-11-30
- 这个宏可以转换成函数吗? 2022-01-01
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
- 如何在 C++ 中初始化静态常量成员? 2022-01-01
- C++结构和函数声明。为什么它不能编译? 2022-11-07
- 在 C++ 中计算滚动/移动平均值 2021-01-01
- 如何通过C程序打开命令提示符Cmd 2022-12-09
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01