C++ 读取由空格或换行符分隔的输入...?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5738882/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
read input separated by whitespace(s) or newline...?
提问by user618712
I'm grabbing input from a standard input stream. Such as,
我正在从标准输入流中获取输入。如,
1 2 3 4 5
or
或者
1
2
3
4
5
I'm using:
我正在使用:
std::string in;
std::getline(std::cin, in);
But that just grabs upto the newline, correct? How can I get input whether they are separated by newline OR whitespace(s) using only iosteam, string, and cstdlib?
但这只是抓住了换行符,对吗?如何仅使用 iosteam、string 和 cstdlib 获取输入是否由换行符或空格分隔?
回答by Tony Delroy
Just use:
只需使用:
your_type x;
while (std::cin >> x)
{
// use x
}
operator>>
will skip whitespace by default. You can chain things to read several variables at once:
operator>>
默认情况下将跳过空格。您可以链接事物以一次读取多个变量:
if (std::cin >> my_string >> my_number)
// use them both
getline()
reads everything on a single line, returning that whether it's empty or contains dozens of space-separated elements. If you provide the optional alternative delimiter ala getline(std::cin, my_string, ' ')
it still won't do what you seem to want, e.g. tabs will be read into my_string
.
getline()
读取一行上的所有内容,无论它是空的还是包含数十个空格分隔的元素,都将返回。如果您提供可选的替代分隔符 alagetline(std::cin, my_string, ' ')
它仍然不会做您想做的事情,例如选项卡将被读入my_string
.
Probably not needed for this, but a fairly common requirement that you may be interested in sometime soon is to read a single newline-delimited line, then split it into components...
可能不需要这样做,但您可能很快会感兴趣的一个相当普遍的要求是读取一个换行符分隔的行,然后将其拆分为组件......
std::string line;
while (std::getline(std::cin, line))
{
std::istringstream iss(line);
first_type first_on_line;
second_type second_on_line;
third_type third_on_line;
if (iss >> first_on_line >> second_on_line >> third_on_line)
...
}
回答by Potatoswatter
Use 'q'
as the the optional argument to getline
.
使用'q'
的可选参数getline
。
#include <iostream>
#include <sstream>
int main() {
std::string numbers_str;
getline( std::cin, numbers_str, 'q' );
int number;
for ( std::istringstream numbers_iss( numbers_str );
numbers_iss >> number; ) {
std::cout << number << ' ';
}
}
回答by Tanner
std::getline( stream, where to?, delimiter ie
std::getline( 流,到哪里?,分隔符即
std::string in;
std::getline(std::cin, in, ' '); //will split on space
or you can read in a line, then tokenize it based on whichever delimiter you wish.
或者您可以读一行,然后根据您希望的任何分隔符对其进行标记。
回答by mihai
the user pressing enter or spaces is the same.
用户按回车或空格是一样的。
int count = 5;
int list[count]; // array of known length
cout << "enter the sequence of " << count << " numbers space separated: ";
// user inputs values space separated in one line. Inputs more than the count are discarded.
for (int i=0; i<count; i++) {
cin >> list[i];
}
回答by AKhanna
int main()
{
int m;
while(cin>>m)
{
}
}
This would read from standard input if it space separated or line separated .
如果空格分隔或行分隔,这将从标准输入读取。
回答by furas
#include <iostream>
using namespace std;
string getWord(istream& in)
{
int c;
string word;
// TODO: remove whitespace from begining of stream ?
while( !in.eof() )
{
c = in.get();
if( c == ' ' || c == '\t' || c == '\n' ) break;
word += c;
}
return word;
}
int main()
{
string word;
do {
word = getWord(cin);
cout << "[" << word << "]";
} while( word != "#");
return 0;
}