什么是scan analougus的scanf格式化输入? [英] What is the cin analougus of scanf formatted input?

查看:149
本文介绍了什么是scan analougus的scanf格式化输入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于scanf,通常有一种直接的格式化输入方式:

With scanf there's, usually, a direct way to take formatted input:

1)使用大于0且小于1的实数进行排序。 'x',例如: 0.32432523x

1) line with a real number higher than 0, and less than 1. Ending on 'x', e.g: 0.32432523x

scanf("0.%[0-9]x", &number);

2)行表示格式为的加法:30 + 28 = / em>

2) line represents an addition in the format :30+28=fifty-eight

scanf(":%d+%d=%99s", &number1, &number2, &total);

cin解决方案是什么,只使用标准库?

What is the cin solution, using only the standard library?

推荐答案

使用>>运算符从cin读取。

Use the >> operator to read from cin.

  int number1, number2;
  std::string text;
  char plus, equals;
  std::cin >> number1 >> plus >> number2 >> equals >> text;
  if (!std::cin.fail() && plus == '+' && equals == '=' && !text.empty())
    std::cout << "matched";

它不如scanf那么好,因为你必须验证scanf字符串中的任何文字自己。使用stream实现比scanf更多的代码行。

It's not as good as scanf because you'd have to verify any literals that were in the scanf string yourself. Doing it with streams will almost certainly be a lot more lines of code than scanf.

我会使用scanf。

这篇关于什么是scan analougus的scanf格式化输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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