I'm trying to do a simple task of reading space separated numbers from console into a vector<int>
, but I'm not getting how to do this properly.
我尝试做一个简单的任务,从控制台读取分隔的数字到向量
This is what I have done till now:
这就是我现在所做的:
int n = 0;
vector<int> steps;
while(cin>>n)
{
steps.push_back(n);
}
However, this requires the user to press an invalid character (such as a
) to break the while
loop. I don't want it.
但是,这需要用户按一个无效字符(如a)来中断while循环。我不想要它。
As soon as user enters numbers like 0 2 3 4 5
and presses Enter
I want the loop to be broken. I tried using istream_iterator
and cin.getline
also, but I couldn't get it working.
一旦用户输入诸如0 2 3 4 5之类的数字并按Enter,我希望循环被破坏。我尝试使用istream_iterator和cin。getline也是,但是我不能让它工作。
I don't know how many elements user will enter, hence I'm using vector
.
我不知道用户会输入多少元素,因此我使用向量。
Please suggest the correct way to do this.
请建议正确的做法。
5 个解决方案
#1
11
Use a getline
combined with an istringstream
to extract the numbers.
使用一个getline和一个istringstream来提取数据。
std::string input;
getline(cin, input);
std::istringstream iss(input);
int temp;
while(iss >> temp)
{
yourvector.push_back(temp);
}
#2
7
To elaborate on jonsca's answer, here is one possibility, assuming that the user faithfully enters valid integers:
要详细说明jonsca的答案,这里有一种可能性,假设用户忠实地输入有效整数:
string input;
getline(cin, input);
istringstream parser(input);
vector<int> numbers;
numbers.insert(numbers.begin(),
istream_iterator<int>(parser), istream_iterator<int>());
This will correctly read and parse a valid line of integers from cin
. Note that this is using the free function getline
, which works with std::string
s, and not istream::getline
, which works with C-style strings.
这将正确读取和解析cin中的有效整数行。注意,这里使用的是免费函数getline,它与std::string一起工作,而不是与c风格的字符串一起工作的istream::getline。
#3
2
This code should help you out, it reads a line to a string and then iterates over it getting out all numbers.
这段代码应该可以帮助您,它将一行读入一个字符串,然后迭代它得到所有的数字。
#include <iostream>
#include <sstream>
#include <string>
int main() {
std::string line;
std::getline(std::cin, line);
std::istringstream in(line, std::istringstream::in);
int n;
vector<int> v;
while (in >> n) {
v.push_back(n);
}
return 0;
}
#4
2
Also, might be helpful to know that you can stimulate an EOF - Press 'ctrl-z' (windows only, unix-like systems use ctrl-d) in the command line, after you have finished with your inputs. Should help you when you're testing little programs like this - without having to type in an invalid character.
同样,如果您知道您可以在完成输入后,在命令行中激发EOF - Press 'ctrl-z'(仅在windows中,类unix系统使用ctrl-d),这可能会有所帮助。当你在测试像这样的小程序时应该会有所帮助——而不需要输入无效的字符。
#5
1
Prompt user after each number or take number count in advance and loop accordingly. Not a great idea but i saw this in many applications.
提示用户在每一个数字后或提前取数字计数,并相应循环。这不是个好主意,但我在很多应用中都看到了。
#1
11
Use a getline
combined with an istringstream
to extract the numbers.
使用一个getline和一个istringstream来提取数据。
std::string input;
getline(cin, input);
std::istringstream iss(input);
int temp;
while(iss >> temp)
{
yourvector.push_back(temp);
}
#2
7
To elaborate on jonsca's answer, here is one possibility, assuming that the user faithfully enters valid integers:
要详细说明jonsca的答案,这里有一种可能性,假设用户忠实地输入有效整数:
string input;
getline(cin, input);
istringstream parser(input);
vector<int> numbers;
numbers.insert(numbers.begin(),
istream_iterator<int>(parser), istream_iterator<int>());
This will correctly read and parse a valid line of integers from cin
. Note that this is using the free function getline
, which works with std::string
s, and not istream::getline
, which works with C-style strings.
这将正确读取和解析cin中的有效整数行。注意,这里使用的是免费函数getline,它与std::string一起工作,而不是与c风格的字符串一起工作的istream::getline。
#3
2
This code should help you out, it reads a line to a string and then iterates over it getting out all numbers.
这段代码应该可以帮助您,它将一行读入一个字符串,然后迭代它得到所有的数字。
#include <iostream>
#include <sstream>
#include <string>
int main() {
std::string line;
std::getline(std::cin, line);
std::istringstream in(line, std::istringstream::in);
int n;
vector<int> v;
while (in >> n) {
v.push_back(n);
}
return 0;
}
#4
2
Also, might be helpful to know that you can stimulate an EOF - Press 'ctrl-z' (windows only, unix-like systems use ctrl-d) in the command line, after you have finished with your inputs. Should help you when you're testing little programs like this - without having to type in an invalid character.
同样,如果您知道您可以在完成输入后,在命令行中激发EOF - Press 'ctrl-z'(仅在windows中,类unix系统使用ctrl-d),这可能会有所帮助。当你在测试像这样的小程序时应该会有所帮助——而不需要输入无效的字符。
#5
1
Prompt user after each number or take number count in advance and loop accordingly. Not a great idea but i saw this in many applications.
提示用户在每一个数字后或提前取数字计数,并相应循环。这不是个好主意,但我在很多应用中都看到了。