C + + input multiple lines of numbers to an array
The day before yesterday to do a company pen test, its input format is a number of lines, each with a space separator, in line with the end of the symbol input to more than one array. In Java, there are corresponding functions to split a row of groups directly, feeling in C + + This input method is still quite strange, today come up with a solution.
Ideas:
Each time a character is read, the judge is not EOF, and if it is, jump out of the loop;
Not EOF to put the character back into the buffer;
Read the line break, the end of line, processing array;
Read the character, if not a space, into a temporary string;
If it is a space, the string is converted into an integer and pressed into the array;
Attention:
For continuous space input to be judged, or you will enter a heap of 0;
The last temporary string is stored in the array when the line break is read;
Code:
#include <iostream>
#include <string>
#include <vector>
#include <cstdlib>
using namespace std;
int main () {
char flag;
while ((Flag=getchar ())!=eof) {
putchar (flag);
string tmpstr;
vector<int> Buff;
char c;
while ((c = GetChar ())!= ' \ n ') {
if (c!= ')
Tmpstr.push_back (c);
else {
if (tmpstr!= "") {
buff.push_back (atoi (Tmpstr.c_str ()));
Tmpstr = "";
}}} if (tmpstr!= "")
Buff.push_back (Atoi (Tmpstr.c_str ()));
for (auto A:buff)
cout << a << ";
cout << ' \ n ';
}
}
Thank you for reading, I hope to help you, thank you for your support for this site!