一行上有多个输入 [英] Multiple inputs on one line

查看:162
本文介绍了一行上有多个输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我没有用,我恐怕这可能是一个简单的问题,没人敢问。



可以输入多个东西标准输入在一行?我的意思是:

  float a,b; 
char c;

//可以安全地假设a,b,c会在float,float,char形式?
cin>> a>> b>> C;


解决方案

$ c> cin ,使用你所描述的语法。结果基本上与:

  cin>>一个; 
cin>> b;
cin>> C;

这是由于一种称为操作符链接的技术。



每次调用 operator>>(istream& T)(其中 T 任意类型)返回对其第一个参数的引用。所以 cin>> a 返回 cin ,可用作(cin>> a)>> b operator>>(istream& T) >首先消耗所有空白字符,然后使用满足输入操作所需的字符数,直到(但不包括)第一个下一个空格字符,无效字符或EOF。


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;

解决方案

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".

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.

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.

这篇关于一行上有多个输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆