Re: Create a list of numbers

From: Michael Kurz (mkurz_at_move-multimedia.com)
Date: 10/27/04


Date: Wed, 27 Oct 2004 20:23:43 +0200


"Eric Lilja" <ericlilja_remove_this@yahoo.com> schrieb im Newsbeitrag
news:cloact$cvk$1@news.island.liu.se...
> 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);
> }
> }
> }
>

Remark:
I would create a class, which is able to parse a string and seperate it into
parts.
As IMHO this task is needed very very often. Something like the split()
function in perl. We inhouse have a class named LineParser, which does this.
Whenever you want to split up strings with some seperation character using
such a spliiter class is much easier and with less probability for errors,
if the class is working properly once.

Regards
Michael