如何从标准输入读取输入,直到按下空格或制表符? [英] How do I read input from standard input until a space or a tab is pressed?
本文介绍了如何从标准输入读取输入,直到按下空格或制表符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要从标准输入读取输入,直到按下空格或TAB。
最后,我需要将输入保存在
因为一个标签计为 whitespace
em>,你要求从流中提取第一个令牌。这在C ++中很容易,因为令牌提取已经是>>
运算符的默认行为: #include< iostream>
#include< string>
int main()
{
std :: string token;
if(!(std :: cin>> token)){/ * error * / return 1; }
//现在你在token中有第一个令牌。
}
(错误只能出现在奥术情况下,例如当输入文件描述符已在程序启动时关闭。)
I need to read input from standard input until a space or a TAB is pressed.
Eventually, I need to hold the input in a std::string
object.
解决方案
Since a tab counts as whitespace, you are asking to extract the first token from the stream. This is very easy in C++, as token extraction is already the default behaviour of the >>
operator:
#include <iostream>
#include <string>
int main()
{
std::string token;
if (!(std::cin >> token)) { /* error */ return 1; }
// now you have the first token in "token".
}
(The error can only occur in arcane circumstances, e.g. when the input file descriptor is already closed when the program starts.)
这篇关于如何从标准输入读取输入,直到按下空格或制表符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文