E
Eric Lilja
Hello, I'm working on my C++ homework and I have a question about the
following function.
It's part of my code the handles command line arguments. Here's the code:
static void
extract_numbers(const char* optarg, vector<string>& numbers)
{
const char* str = optarg;
while(true)
{
string s(str);
string::size_type index = s.find_first_of(',');
if(index == string::npos)
{
string temp(s.c_str(), s.length());
numbers.push_back(temp);
break;
}
else
{
string temp(s.c_str(), index);
numbers.push_back(temp);
str += (index + 1);
}
}
}
optarg points to a null-terminated string of the following form:
somenumber1,somenumber2,somenumber3
where somenumber* is a, *drumroll*, number. It needs to extract these
numbers and put them in the vector. Note that
optarg may point to anything, I haven't made any sanity checks on the input
at this point and it may also
contain only one number. My way seems to work for "friendly" input, is it ok
to hand in as it is (I need to perform better on
this assignment) or can it be improved/bug fixed?
Thanks for any help!
/ Eric
following function.
It's part of my code the handles command line arguments. Here's the code:
static void
extract_numbers(const char* optarg, vector<string>& numbers)
{
const char* str = optarg;
while(true)
{
string s(str);
string::size_type index = s.find_first_of(',');
if(index == string::npos)
{
string temp(s.c_str(), s.length());
numbers.push_back(temp);
break;
}
else
{
string temp(s.c_str(), index);
numbers.push_back(temp);
str += (index + 1);
}
}
}
optarg points to a null-terminated string of the following form:
somenumber1,somenumber2,somenumber3
where somenumber* is a, *drumroll*, number. It needs to extract these
numbers and put them in the vector. Note that
optarg may point to anything, I haven't made any sanity checks on the input
at this point and it may also
contain only one number. My way seems to work for "friendly" input, is it ok
to hand in as it is (I need to perform better on
this assignment) or can it be improved/bug fixed?
Thanks for any help!
/ Eric