问题
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.
回答1:
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.
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;
}
回答2:
C++
If you have an unknown number of entries spread across an unknown number of lines, ending at 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);
}
}
回答3:
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;
}
回答4:
In C++, you can use 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 >> a >> b >> c >> d;
回答5:
The quick solution is to read them with scanf()
int array[1000];
int index = 0;
while ((index < 1000) && (scanf("%d", &tmp) == 1)) {
array[index++] = tmp;
}
This still needs a bit more validation ...
回答6:
C++:
vector<int> ints;
while( !cin.eof() )
{
int t;
cin >> t;
if ( !cin.eof() )
ints.push_back(t);
}
Alternative (thx to Shahbaz)
int t;
vector<int> ints;
while(cin >> t)
ints.push_back(t);
回答7:
In C++ it's extremely simple to read N integers separated by whitespace via stdin:
#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;
}
来源:https://stackoverflow.com/questions/7800638/how-to-read-n-integers-from-standard-input-in-c