explode equivalent in c++

Discussion in 'C++' started by DJH, Jun 19, 2007.

  1. DJH

    DJH Guest

    I am trying to find the most efficient way to code a php explode in C++.

    What existing STL function may I use to separate this in to its parts
    where the delimiter is "||" ?

    example:

    ~!||1||1||foobar

    I would like a string array as such

    stringarray[0]="~!"
    stringarray[1]="1"
    stringarray[2]="1"
    stringarray[3]="foobar"

    Thanks in advance!
    DJH, Jun 19, 2007
    #1
    1. Advertising

  2. DJH wrote:
    > I am trying to find the most efficient way to code a php explode in C++.
    >
    > What existing STL function may I use to separate this in to its parts
    > where the delimiter is "||" ?
    >
    > example:
    >
    > ~!||1||1||foobar
    >
    > I would like a string array as such
    >
    > stringarray[0]="~!"
    > stringarray[1]="1"
    > stringarray[2]="1"
    > stringarray[3]="foobar"
    >
    > Thanks in advance!


    Exactly what kind of efficiency are you talking about ?

    #include <string>

    const std::string arrayss[] = { "~!", "1", "1", "foobar" };

    It's debatable what is more efficient than what - however this below is
    about as efficient as you can get:

    const char * const arrayccs[] = { "~!", "1", "1", "foobar" };

    Since there is no run-time cost for arrayccs.

    But the real question is then how it's used. If you're taking
    std::string s of strings from arrayccs all the time it may be more
    efficient to use arrayss.

    Having said all this, I fail to see why it matters so much since unless
    you can measure the difference, it's irrelevant.
    Gianni Mariani, Jun 19, 2007
    #2
    1. Advertising

  3. DJH

    DJH Guest

    Gianni Mariani wrote:
    > DJH wrote:
    >> I am trying to find the most efficient way to code a php explode in C++.
    >>
    >> What existing STL function may I use to separate this in to its parts
    >> where the delimiter is "||" ?
    >>
    >> example:
    >>
    >> ~!||1||1||foobar
    >>
    >> I would like a string array as such
    >>
    >> stringarray[0]="~!"
    >> stringarray[1]="1"
    >> stringarray[2]="1"
    >> stringarray[3]="foobar"
    >>
    >> Thanks in advance!

    >
    > Exactly what kind of efficiency are you talking about ?



    I just need to parse the "~!||1||1||foobar" string into parts delimited
    by the "||" delimiter, thats all. The parts should be a std::string array.


    >
    > #include <string>
    >
    > const std::string arrayss[] = { "~!", "1", "1", "foobar" };
    >
    > It's debatable what is more efficient than what - however this below is
    > about as efficient as you can get:
    >
    > const char * const arrayccs[] = { "~!", "1", "1", "foobar" };
    >
    > Since there is no run-time cost for arrayccs.
    >
    > But the real question is then how it's used. If you're taking
    > std::string s of strings from arrayccs all the time it may be more
    > efficient to use arrayss.
    >
    > Having said all this, I fail to see why it matters so much since unless
    > you can measure the difference, it's irrelevant.
    DJH, Jun 19, 2007
    #3
  4. DJH

    Alan Johnson Guest

    DJH wrote:
    > I am trying to find the most efficient way to code a php explode in C++.
    >
    > What existing STL function may I use to separate this in to its parts
    > where the delimiter is "||" ?
    >
    > example:
    >
    > ~!||1||1||foobar
    >
    > I would like a string array as such
    >
    > stringarray[0]="~!"
    > stringarray[1]="1"
    > stringarray[2]="1"
    > stringarray[3]="foobar"
    >
    > Thanks in advance!


    Writing an explode function should not be too difficult. E.g.:
    #include <vector>
    #include <string>

    std::vector<std::string> explode(
    const std::string & in,
    const std::string & delim)
    {
    typedef std::string::size_type size_type ;

    const size_type delim_len = delim.length() ;
    std::vector<std::string> result ;

    size_type i = 0, j ;
    for (;;)
    {
    j = in.find(delim, i) ;
    result.push_back(in.substr(i, j-i)) ;
    if (j == std::string::npos)
    break ;
    i = j + delim_len ;
    }

    return result ;
    }

    #include <iterator>
    #include <algorithm>
    #include <iostream>

    int main()
    {
    std::vector<std::string> parts = explode("~!||1||1||foobar", "||") ;
    std::copy(parts.begin(), parts.end(),
    std::eek:stream_iterator<std::string>(std::cout, "\n")) ;
    }

    --
    Alan Johnson
    Alan Johnson, Jun 19, 2007
    #4
  5. DJH

    Alan Johnson Guest

    On Jun 18, 4:35 pm, DJH <> wrote:
    > I am trying to find the most efficient way to code a php explode in C++.
    >
    > What existing STL function may I use to separate this in to its parts
    > where the delimiter is "||" ?
    >
    > example:
    >
    > ~!||1||1||foobar
    >
    > I would like a string array as such
    >
    > stringarray[0]="~!"
    > stringarray[1]="1"
    > stringarray[2]="1"
    > stringarray[3]="foobar"
    >
    > Thanks in advance!



    It shouldn't be hard to cook up an explode function. Here is what I
    come up with after a few minutes of tinkering:
    #include <vector>
    #include <string>

    std::vector<std::string> explode(
    const std::string & in,
    const std::string & delim)
    {
    typedef std::string::size_type size_type ;

    const size_type delim_len = delim.length() ;
    std::vector<std::string> result ;

    size_type i = 0, j ;
    for (;;)
    {
    j = in.find(delim, i) ;
    result.push_back(in.substr(i, j-i)) ;
    if (j == std::string::npos)
    break ;
    i = j + delim_len ;
    }

    return result ;
    }

    #include <iterator>
    #include <algorithm>
    #include <iostream>

    int main()
    {
    std::vector<std::string> parts = explode("~!||1||1||foobar",
    "||") ;
    std::copy(parts.begin(), parts.end(),
    std::eek:stream_iterator<std::string>(std::cout, "\n")) ;
    }

    --
    Alan Johnson
    Alan Johnson, Jun 19, 2007
    #5
  6. DJH

    James Kanze Guest

    On Jun 19, 1:35 am, DJH <> wrote:
    > I am trying to find the most efficient way to code a php explode in C++.


    > What existing STL function may I use to separate this in to its parts
    > where the delimiter is "||" ?


    > example:


    > ~!||1||1||foobar


    > I would like a string array as such


    > stringarray[0]="~!"
    > stringarray[1]="1"
    > stringarray[2]="1"
    > stringarray[3]="foobar"


    You might want to look at the code for FieldArray and its
    subclasses in the Text subsystem at my site
    (http://kanze.james.neuf.fr/code-en.html). You'll probably have
    to adapt it some; it's old code, and for example,
    RegularExpressionSeparatedFields uses my own RegularExpression
    class, rather than that of Boost. But it's simple to use:

    RegularExpressionSeparatedFields
    fields( "\\|\\|" ) ;
    std::string line ;
    while ( std::getline( in, line ) ) {
    fields = line ;
    // fields[0] contains the entire line, fields[1] the
    // first field, fields[2] the second, etc.
    }

    The base class FieldArray uses the strategy pattern to decide
    how to cut up the string, so it would be easy to add additional
    critera. (All RegularExpressionSeparatedFields does in addition
    is to provide the delegate.) It should be simple, for example,
    to provide a delegate which uses a fixed string as a separator;
    just replace std::find in CharacterSeparatedFieldSplitter with
    std::search.

    --
    James Kanze (GABI Software, from CAI) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, Jun 19, 2007
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. JKop

    BEFORE I EXPLODE

    JKop, Sep 18, 2004, in forum: C++
    Replies:
    17
    Views:
    879
    Old Wolf
    Sep 20, 2004
  2. AceHigh

    Pelles C POLIB.EXE /EXPLODE utility

    AceHigh, Sep 23, 2005, in forum: C Programming
    Replies:
    3
    Views:
    787
    jacob navia
    Sep 25, 2005
  3. cc
    Replies:
    0
    Views:
    300
  4. cc
    Replies:
    1
    Views:
    386
    Niels Dybdahl
    Jun 22, 2005
  5. Replies:
    13
    Views:
    342
Loading...

Share This Page