如何从 C++ 的标准输入中读取 n 个整数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7800638/
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
How to read n integers from standard input in C++?
提问by Topo
I need to read something like:
我需要阅读以下内容:
5 60 35 42
2 38 6
5 8
300 1500 900
And then save the first line in an array. After calling other functions do the same with the next line, and so on.
然后将第一行保存在数组中。调用其他函数后,对下一行执行相同操作,依此类推。
I try with gets()
and then use sscanf()
to scan the integers from the string, but I don't know how to read n numbers from a string.
我尝试使用gets()
然后使用sscanf()
从字符串中扫描整数,但我不知道如何从字符串中读取 n 个数字。
采纳答案by Mooing Duck
I've seen input files like this for competitions before. If speed is more of an issue than error detection, you could use a custom routine. Here's one similar to that I use:
我以前见过这样的比赛输入文件。如果速度比错误检测更重要,您可以使用自定义例程。这是一个类似于我使用的:
void readintline(unsigned int* array, int* size) {
char buffer[101];
size=0;
char* in=buffer;
unsigned int* out=array;
fgets(buffer, 100, stdin);
do {
*out=0;
while(*in>='0') {
*out= *out* 10 + *in-'0';
++in;
}
if (*in)
++in; //skip whitespace
++out;
} while(*in);
size = out-array;
}
It will destroy your memory if there's more than 100 characters on a line, or more numbers than array can hold, but you won't get a faster routine to read in lines of unsigned ints.
如果一行中有超过 100 个字符,或者比数组可以容纳的数字更多,它会破坏您的内存,但是您将无法获得更快的例程来读取无符号整数行。
On the other hand, if you want simple:
另一方面,如果你想要简单:
int main() {
std::string tmp;
while(std::getline(std::cin, tmp)) {
std::vector<int> nums;
std::stringstream ss(tmp);
int ti;
while(ss >> ti)
nums.push_back(ti);
//do stuff with nums
}
return 0;
}
回答by Rob?
C++
C++
If you have an unknown number of entries spread across an unknown number of lines, ending at EOF:
如果您有未知数量的条目分布在未知数量的行中,以 EOF 结尾:
int n;
while(cin >> n)
vector_of_int.push_back(n);
If you have a known number of entries spread across an unknown number of lines:
如果您有已知数量的条目分布在未知数量的行中:
int n;
int number_of_entries = 20; // 20 for example, I don't know how many you have.
for(int i ; i < number_of_entries; ++i)
if(cin >> n)
vector_of_int.push_back(n);
If you have an uknown number of entries on a single line:
如果您在一行中有未知数量的条目:
std::string str;
std::getline(std::cin, str);
std::istringstream sstr(str);
int n;
while(sstr >> n)
vector_of_int.push_back(n);
If you have a unknown number of entries spread across a known number of lines:
如果您有未知数量的条目分布在已知数量的行中:
for(int i = 0; i < number_of_lines; ++i) {
std::string str;
if(std::getline(std::cin, str)) {
std::istringstream sstr(str);
int n;
while(sstr >> n)
vector_of_int.push_back(n);
}
}
回答by Jerry Coffin
I'd probably write the code something like this:
我可能会写这样的代码:
// Warning: untested code.
std::vector<int> read_line_ints(std::istream &is) {
std::string temp;
std::getline(is, temp);
std::istringstream buffer(temp);
int num;
std::vector<int> ret;
while (buffer>>num)
ret.push_back(num);
return ret;
}
回答by Peter Alexander
In C++, you can use std::istringstream
.
在 C++ 中,您可以使用std::istringstream
.
std::string nums = "1 20 300 4000";
std::istringstream stream(nums);
int a, b, c, d;
stream >> a >> b >> c >> d;
assert(a == 1 && b == 20 && c == 300 && d == 4000);
If you want to get it from the standard input, then do the same, but just use std::cin
如果您想从标准输入中获取它,请执行相同操作,但只需使用 std::cin
std::cin >> a >> b >> c >> d;
回答by pmg
回答by George Kastrinis
C++:
C++:
vector<int> ints;
while( !cin.eof() )
{
int t;
cin >> t;
if ( !cin.eof() )
ints.push_back(t);
}
Alternative (thx to Shahbaz)
替代方案(感谢 Shahbaz)
int t;
vector<int> ints;
while(cin >> t)
ints.push_back(t);
回答by AusCBloke
In C++ it's extremely simple to read N integers separated by whitespace via stdin:
在 C++ 中,通过 stdin 读取由空格分隔的 N 个整数非常简单:
#include <iostream>
using namespace std;
const unsigned N = 5;
int main(void)
{
int nums[N];
for (unsigned i = 0; i < N; ++i)
cin >> nums[i];
cout << "Your numbers were:\n";
for (unsigned i = 0; i < N; ++i)
cout << nums[i] << " ";
cout << "\n";
return 0;
}