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



Relevant Pages

  • Re: Create a list of numbers
    ... Eric Lilja wrote: ... > It's part of my code the handles command line arguments. ... erroneous input is in the original string. ...
    (comp.lang.cpp)
  • TIP #185: Null Handling
    ... nulls, and command modifications for manipulating them. ... Tcl deals with strings, the universal medium for representing data. ... is know and it is an empty string, but if a respondent forgets to give ...
    (comp.lang.tcl)
  • Re: Determine how Word was launched
    ... inspect the command line used to launch Word. ... Private Declare Function GetCommandLineA Lib "kernel32" As Long ... Public Function GetCommandLineAs String ... Dim lngCmdLinePtr As Long ...
    (microsoft.public.office.developer.vba)
  • Re: Class.getMethod in classs static initializer block
    ... I just wanted to add that I had some similar code for a "command line server" project. ... static private final boolean DEBUG = true; ... {public ReturnCode runCommand(String s) ... public ReturnCode parseLine(String line) ...
    (comp.lang.java.programmer)
  • Re: New "base document" available
    ... How to go about retrieving command line parameters? ... the command line arguments (and environment variables) ... Keyword and positional parameters can be mixed in the same command line. ... clp-name points to a string containing "P1" ...
    (comp.lang.cobol)