本文介紹了scanf 格式輸入的 cin 類比是什么?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
使用 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=58
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 字符串中的任何文字.用流來做這件事幾乎肯定會比 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.
I would use scanf.
這篇關于scanf 格式輸入的 cin 類比是什么?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!