Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python

equiv from python

 

 

Python python RSS feed   Index | Next | Previous | View Threaded


strombrg at seki

Aug 28, 2001, 2:25 PM

Post #1 of 4 (292 views)
Permalink
equiv from python

Does c++ have an equivalent to this from python:

>>> import string
>>> s='ab:cd:efg:ghi:'
>>> print string.splitfields(s,':')
['ab', 'cd', 'efg', 'ghi', '']

IE, can the standard c++ library take a string, split it up on a
particular separator character, and return the corresponding list of
strings?

I know I can write something to do this, but I'd rather learn how to
use the STL or similar, if applicable.

--
Dan Stromberg UCI/NACS/DCS


ignacio at openservices

Aug 28, 2001, 2:54 PM

Post #2 of 4 (268 views)
Permalink
equiv from python [In reply to]

On 28 Aug 2001, Dan Stromberg wrote:

> Does c++ have an equivalent to this from python:
>
> >>> import string
> >>> s='ab:cd:efg:ghi:'
> >>> print string.splitfields(s,':')
> ['ab', 'cd', 'efg', 'ghi', '']
>
> IE, can the standard c++ library take a string, split it up on a
> particular separator character, and return the corresponding list of
> strings?
>
> I know I can write something to do this, but I'd rather learn how to
> use the STL or similar, if applicable.

Not with string per se, but you can do it by using for_each() on a
vector<char> and adding the vector<char> chunk to a list<vector<char> > when
you encounter a colon.

--
Ignacio Vazquez-Abrams <ignacio [at] openservices>


bk at xk7

Aug 29, 2001, 6:19 AM

Post #3 of 4 (261 views)
Permalink
equiv from python [In reply to]

> > >>> import string
> > >>> s='ab:cd:efg:ghi:'
> > >>> print string.splitfields(s,':')
> > ['ab', 'cd', 'efg', 'ghi', '']
> >
> > IE, can the standard c++ library take a string, split it up on a
> > particular separator character, and return the corresponding list of
> > strings?
Boost (http:/www.boost.org) has, among lots of other useful libraries, a
tokenizer. Boost is nice for this kind of stuff becausse
* the code is good
* it integrates well with the standard library
* it has a good chance of making it into the standard next time.

HTH,

Burkhard


grelbr at hotmail

Aug 29, 2001, 7:09 AM

Post #4 of 4 (264 views)
Permalink
equiv from python [In reply to]

strombrg [at] seki (Dan Stromberg) wrote in message news:<9mh27s$dmi$1 [at] news>...
[snip]
> IE, can the standard c++ library take a string, split it up on a
> particular separator character, and return the corresponding list of
> strings?
>
> I know I can write something to do this, but I'd rather learn how to
> use the STL or similar, if applicable.

Well, there's strtok(). But, I personally think the best use of that
function is as a teacher of the unwary. Before you elect to use that
function, make sure you read the *entire* description in your docs,
all the way to the end and every word. Be sure to pay attention to
the fact that there is a static storage space involved, and be sure
you know what that implies.

Maybe I sound pretty grumpy on this point. But that's because, some
years ago, I blindly used strtok() without paying attention to that
stuff. Don't you be like I was. Get smart sooner rather than later.
grelbr

Python python RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact lists@gossamer-threads.com
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.