tokenising a C++ string class

tokenising a C++ string class

Post by Roger Leig » Sat, 26 Apr 2003 07:26:16



I have a class to hold configuration data.  Each item has a key and
associated value, both strings.  The key is in the form
"foo::bar::name::space", and the key will be stored in a tree like
this (each level is an instance of the class with a vector holding
child members):

foo
`-bar
  `-name
    `-space = value

To look up a value, (string val =
config.lookup("foo::bar::name::space")), I need to split up
"foo::bar::name::space" into "foo" and "bar::name::space".  Once foo
is found, the function recurses until no tokens are left, and then the
value is returned.  If the key is invalid, an exception is thrown.

What is the "C++ way" to separate the tokens?  Coming from C, I can
think of plenty of ways to do it, but there must be a clean and simple
way.  From what I've read, istringstreams are a nice approach, but I'm
not sure how to pull out only to the delimiter, discard the delimiter
and so on.

Alternatively, string::find("::") can tell me where the first
delimiter is, and then I can use string::substr to split it up.  It's
not clear what's returned if delim isn't found, though (WTH is
npos??), or how it's different to find_first_of().

Could anyone recommend a good guide (either book or on-line) to
libstdc++ and the STL?  The SGI STL guide doesn't go into much depth,
nd doesn't document all the standard library, and the GCC doxygen docs
are quite incomplete (my libstdc++5 docs no longer mention
std::basic_string, and the libstdc++3 version has no descriptions).
I've got a couple of books, but they only describe the language, and
don't go into iostreams, streambufs, strings, STL etc.

Thanks,
Roger

--
Roger Leigh

                Printing on GNU/Linux?  http://gimp-print.sourceforge.net/
                GPG Public Key: 0x25BFB848 available on public keyservers

 
 
 

tokenising a C++ string class

Post by André P?nit » Sat, 26 Apr 2003 20:01:30



> Alternatively, string::find("::") can tell me where the first
> delimiter is, and then I can use string::substr to split it up.  It's
> not clear what's returned if delim isn't found, though

std::string::npos

Quote:> (WTH is npos??), or how it's different to find_first_of().

    template<class charT, .....>
    class basic_string {
       static const size_type npos = -1;

    typedef basic_string<char> string;

so   std::string::npos == std::string::size_type(-1)

Quote:> Could anyone recommend a good guide (either book or on-line) to
> libstdc++ and the STL?

Jossuttis' STL book perhaps?

Andre'

--
Those who desire to give up Freedom in order to gain Security, will not have,
nor do they deserve, either one.     (T. Jefferson or B. Franklin or both...)

 
 
 

tokenising a C++ string class

Post by Julián Alb » Sat, 26 Apr 2003 23:42:25


Roger Leigh escribi:

Quote:> Alternatively, string::find("::") can tell me where the first
> delimiter is, and then I can use string::substr to split it up.  It's
> not clear what's returned if delim isn't found, though (WTH is
> npos??), or how it's different to find_first_of().

find return std::string::npos when not found.

Quote:> Could anyone recommend a good guide (either book or on-line) to
> libstdc++ and the STL?  The SGI STL guide doesn't go into much depth,

"The C++ Programming Language", by Stroustrup.

Regards.

 
 
 

tokenising a C++ string class

Post by Alan Giffor » Sun, 27 Apr 2003 06:08:47


I have used a string library made by Alavoor Vasudevan (Al Dev) which worked
quite nicely.  It's very simple, and although I've never used Java, it is
purported to emulate the String functionality therein.  There is a
StringTokenizer object as well, which makes the tokenization you speak of
quite simple.  I've never done it any other way, but I'm a pretty novice
programmer.

This library is simple and very easy to compile.  Check out some in-depth info
on it here:

http://www.linuxselfhelp.com/HOWTO/C++Programming-HOWTO.html

Hope this helps.


> I have a class to hold configuration data.  Each item has a key and
> associated value, both strings.  The key is in the form
> "foo::bar::name::space", and the key will be stored in a tree like
> this (each level is an instance of the class with a vector holding
> child members):

> foo
> `-bar
>   `-name
>     `-space = value

> To look up a value, (string val =
> config.lookup("foo::bar::name::space")), I need to split up
> "foo::bar::name::space" into "foo" and "bar::name::space".  Once foo
> is found, the function recurses until no tokens are left, and then the
> value is returned.  If the key is invalid, an exception is thrown.

> What is the "C++ way" to separate the tokens?  Coming from C, I can
> think of plenty of ways to do it, but there must be a clean and simple
> way.  From what I've read, istringstreams are a nice approach, but I'm
> not sure how to pull out only to the delimiter, discard the delimiter
> and so on.

> Alternatively, string::find("::") can tell me where the first
> delimiter is, and then I can use string::substr to split it up.  It's
> not clear what's returned if delim isn't found, though (WTH is
> npos??), or how it's different to find_first_of().

> Could anyone recommend a good guide (either book or on-line) to
> libstdc++ and the STL?  The SGI STL guide doesn't go into much depth,
> nd doesn't document all the standard library, and the GCC doxygen docs
> are quite incomplete (my libstdc++5 docs no longer mention
> std::basic_string, and the libstdc++3 version has no descriptions).
> I've got a couple of books, but they only describe the language, and
> don't go into iostreams, streambufs, strings, STL etc.

> Thanks,
> Roger

> --
> Roger Leigh

>                 Printing on GNU/Linux?  http://gimp-print.sourceforge.net/
>                 GPG Public Key: 0x25BFB848 available on public keyservers

 
 
 

tokenising a C++ string class

Post by Roger Leig » Sun, 27 Apr 2003 07:51:03




> so   std::string::npos == std::string::size_type(-1)

Thanks.  I've now got it all more-or-less working, using
string::substr().

Quote:> > Could anyone recommend a good guide (either book or on-line) to
> > libstdc++ and the STL?

> Jossuttis' STL book perhaps?

I'll take a look, but the STL isn't all of the standard library.  Who
publishes it?

The libstdc++ docs mention "The C++ Standard Library" by Nicolai
Josuttis (Addison-Wesley).  Can anyone recommend this?

Thanks,
Roger

--
Roger Leigh

                Printing on GNU/Linux?  http://gimp-print.sourceforge.net/
                GPG Public Key: 0x25BFB848 available on public keyservers

 
 
 

tokenising a C++ string class

Post by Roger Leig » Sun, 27 Apr 2003 07:51:04



> Roger Leigh escribi:

> > Alternatively, string::find("::") can tell me where the first
> > delimiter is, and then I can use string::substr to split it up.  It's
> > not clear what's returned if delim isn't found, though (WTH is
> > npos??), or how it's different to find_first_of().

> find return std::string::npos when not found.

Ah, right.  npos == "null position" ?

Quote:> > Could anyone recommend a good guide (either book or on-line) to
> > libstdc++ and the STL?  The SGI STL guide doesn't go into much depth,

> "The C++ Programming Language", by Stroustrup.

That's just the language, not the standard library IIRC (and I want a
book covering the ISO libstdc++).  I have attempted Stroustrup, but
while he might be a brilliant programmer, his writing skills are not
that great.  The K&R C book was a masterpiece--simple and concise,
while covering everything.  Stroustrup dives straight in to complex OO
theory right from the first page, and it's very heavy going.

I'm currently reading "Practical C++ Programming" by Steve Oualline
(O'Reilly, 2003), and I previously read SAMS C++ for Linux
(simplistic, but is solid and covers most of the language).

Thanks,
Roger

--
Roger Leigh

                Printing on GNU/Linux?  http://gimp-print.sourceforge.net/
                GPG Public Key: 0x25BFB848 available on public keyservers

 
 
 

tokenising a C++ string class

Post by Erik Max Franci » Sun, 27 Apr 2003 09:02:33



> The libstdc++ docs mention "The C++ Standard Library" by Nicolai
> Josuttis (Addison-Wesley).  Can anyone recommend this?

In a word, yes.  This is perhaps the finest tutorial and reference
written on the subject ever created.  You will not find a better book on
the subject.

Josuttis has a new book out about templates, which I'm looking forward
to snagging a copy of, but haven't read yet.

--

 __ San Jose, CA, USA / 37 20 N 121 53 W / &tSftDotIotE
/  \ Some mistakes we must carry with us.
\__/ Speaker-to-Animals
    Bosskey.net: Return to Wolfenstein / http://www.bosskey.net/rtcw/
 A personal guide to Return to Castle Wolfenstein.

 
 
 

tokenising a C++ string class

Post by Julián Alb » Mon, 28 Apr 2003 05:06:52


Roger Leigh escribi:

Quote:> > find return std::string::npos when not found.

> Ah, right.  npos == "null position" ?

Not valid Position, I think.

Quote:> > "The C++ Programming Language", by Stroustrup.
> That's just the language, not the standard library IIRC (and I want a

There is a book recommended, called "The C++ Standard Library" or
something similar, but I don't remeber the author. Ask in comp.lang.c++.

Regards.

 
 
 

tokenising a C++ string class

Post by André P?nit » Wed, 30 Apr 2003 18:08:10



>> Jossuttis' STL book perhaps?

> I'll take a look, but the STL isn't all of the standard library.  Who
> publishes it?

> The libstdc++ docs mention "The C++ Standard Library" by Nicolai
> Josuttis (Addison-Wesley).  Can anyone recommend this?

Sorry, I meant that book. I am always a bit sloppy when it comes to the
STL vs C++ Standard Library.

Andre'

--
Those who desire to give up Freedom in order to gain Security, will not have,
nor do they deserve, either one.     (T. Jefferson or B. Franklin or both...)