M
Mike Copeland
How do I (or can I?) parse a variable number of numeric values from a
line of text? Below is an example of a data line I need to parse (14
values, but there can be more or less):
1 37 36 4 7 5 6 8 9 10 20 32 23 16
I'm currently using the routine below, which works in a sense, but I
have to ignore blank values in the output vector. In the example data,
I get 20 vector elements, but I have to ignore 6 of them. 8<{{
typedef vector<string> TOKENS1; // parsing structure
TOKENS1 tokArray;
void parseSpace(string line) // parse & store blank-separated tokens
{
string tok1, tok2;
istringstream iss1(line);
tokArray.clear();
while(getline(iss1, tok1, ' '))
{
if(tok1.find(' ') != string::npos)
{
istringstream iss1(tok1);
while(getline(iss1, tok2, ' '))
{
if(!tok2.empty()) tokArray.push_back(tok2);
} // while
} // if
else tokArray.push_back(tok1);
} // while
return;
} // parseSpace
I assume there is a better way to parse such data (it might involve
stringstream). If so, how would I do such a thing in C++? TIA
line of text? Below is an example of a data line I need to parse (14
values, but there can be more or less):
1 37 36 4 7 5 6 8 9 10 20 32 23 16
I'm currently using the routine below, which works in a sense, but I
have to ignore blank values in the output vector. In the example data,
I get 20 vector elements, but I have to ignore 6 of them. 8<{{
typedef vector<string> TOKENS1; // parsing structure
TOKENS1 tokArray;
void parseSpace(string line) // parse & store blank-separated tokens
{
string tok1, tok2;
istringstream iss1(line);
tokArray.clear();
while(getline(iss1, tok1, ' '))
{
if(tok1.find(' ') != string::npos)
{
istringstream iss1(tok1);
while(getline(iss1, tok2, ' '))
{
if(!tok2.empty()) tokArray.push_back(tok2);
} // while
} // if
else tokArray.push_back(tok1);
} // while
return;
} // parseSpace
I assume there is a better way to parse such data (it might involve
stringstream). If so, how would I do such a thing in C++? TIA