Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

best way to tokenize an std::string?

Name: Anonymous 2006-11-06 20:42

So I need to split a c++ string object based on some delimiters. what strtok() does on c-strings is basicaly what I need to do. I was looking at the stringstream class and was thinking I could use that, but input is always split on whitespace and I can't find a way to tell it my own delimiters.

Besides just calling c_str() and using strtok, how can I easily get it's equilivant?

Name: Anonymous 2006-11-06 23:52

maybe a combination of string.find() and substring()

Name: Anonymous 2006-11-07 11:26

Use the PCRE library.

Name: Anonymous 2006-11-07 11:44

I'd rather suffocate myself with my own tounge than do anything with strings in C/C++/Java

Name: Anonymous 2006-11-07 11:51

>>4
With std::basic_string it's a piece of cake.

C and Java suck though, C less as pointer magic is fun.

Name: Anonymous 2006-11-07 13:09

Try this:

vector<string> tokenise(const string& str, const string& delims)
{
    vector<string> strlist;
    int start, end = 0;

    while((start = str.find_first_not_of(delims, end)) != string::npos)
    {
        end = str.find_first_of(delims, start);
        if (end == string::npos)
        {
            strlist.push_back(str.substr(start));
            return strlist;
        }

        strlist.push_back(str.substr(start, end - start));
    }

    return strlist;
}

Name: Anonymous 2006-11-10 14:27

Name: Anonymous 2006-11-21 15:55

do a barrel roll!

Name: Anonymous 2006-11-23 16:16

Property Name="UnifiedAreaRect" Value="{{left_relative,left_offset},{top_relative,top_offset},{right_relative,right_offset},{bottom_relative,bottom_offset}}" />

the awesomenes of CEGUIs layout syntax > all.
ok, the only other layout syntax i know is css and it sucks so much.

Name: Anonymous 2006-11-24 6:41

CSS > XML
Imagine the pain in the arse it would have been if it had been XML.

Name: Anonymous 2006-11-24 17:35

Everything should be XML. For an example of the superiority of XML, I present my .fonts.conf file:
<?xml version="1.0"?>
<!DOCTYPE fontconfig SYSTEM "fonts.dtd">
<fontconfig>
 <match target="font" >
  <edit mode="assign" name="hinting" >
   <bool>true</bool>
  </edit>
 </match>
 <match target="font" >
  <edit mode="assign" name="hintstyle" >
   <const>hintfull</const>
  </edit>
 </match>
 <match target="font" >
  <edit mode="assign" name="rgba" >
   <const></const>
  </edit>
 </match>
</fontconfig>
See how much more readable, maintainable, scalable and enterprisey this is compared to an ugly hack such as:
hinting=true
hintstyle=hintful
rgba=

Name: Anonymous 2006-11-25 10:47 (sage)

How do I became an std::string splitting hero?

Name: Anonymous 2009-01-14 12:55

LISP

Name: Sgt.Kabuꓖ쵪kimanꡦ傜 2012-05-28 20:49

Bringing /prog/ back to its people
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List