Re: Tokenising a string by \n.

Discussion in 'C++' started by John Harrison, Jul 17, 2003.

  1. "Jason Heyes" <> wrote in message
    news:3f162156$0$1210$...
    > What is the best way to tokenise a string by \n. Here is one way I have
    > thought of
    >
    > vector<string> lines;
    > vector<string>::size_type i, start;
    >
    > start = 0;
    > for (i=0; i < str.size(); i++)
    > {
    > if (str == '\n')
    > {
    > lines.push_back(str.substr(start, i));
    > start = i + 1;
    > }
    > }
    > lines.push_back(str.substr(start, str.size()));
    >
    > A better method would be appreciated.
    >


    Well that depends on what you mean by better.

    ES Kim has shown you are method that uses less code, but even s/he only
    described it as 'more elegant' not better.

    To me what you have looks fine, its efficient and easy to understand. If its
    your style then go for it.

    john
    John Harrison, Jul 17, 2003
    #1
    1. Advertising

  2. >
    > To me what you have looks fine, its efficient and easy to understand. If

    its
    > your style then go for it.
    >
    > john
    >


    Actually the only change I might recommend is to use iterators instead of
    integers to iterate over the string. Using operator[] on a reference counted
    string can be inefficient.

    vector<string> lines;
    vector<string>::const_iterator i, start, end;
    end = str.end();
    start = str.begin();
    for (i=start; i != end; ++i)
    {
    if (*i == '\n')
    {
    lines.push_back(string(start, i));
    start = i + 1;
    }
    }
    lines.push_back(string(start,end));

    Untested code.

    john
    John Harrison, Jul 17, 2003
    #2
    1. Advertising

  3. "John Harrison" <> wrote in message
    news:bf5i66$a7odr$-berlin.de...
    >
    > "Jason Heyes" <> wrote in message
    > news:3f162156$0$1210$...
    > > What is the best way to tokenise a string by \n. Here is one way I have
    > > thought of
    > >
    > > vector<string> lines;
    > > vector<string>::size_type i, start;
    > >
    > > start = 0;
    > > for (i=0; i < str.size(); i++)
    > > {
    > > if (str == '\n')
    > > {
    > > lines.push_back(str.substr(start, i));
    > > start = i + 1;
    > > }
    > > }
    > > lines.push_back(str.substr(start, str.size()));
    > >
    > > A better method would be appreciated.
    > >


    Me again, actually the code you posted is bugged. Although the idea is
    fine, the second parameter to substr is the length of the substring, not the
    end position in the original string (which is what you are assuming).

    john
    John Harrison, Jul 17, 2003
    #3
    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. Mladen Adamovic
    Replies:
    0
    Views:
    718
    Mladen Adamovic
    Dec 4, 2003
  2. Mladen Adamovic
    Replies:
    3
    Views:
    14,546
    Mladen Adamovic
    Dec 5, 2003
  3. Matt
    Replies:
    3
    Views:
    470
    Tor Iver Wilhelmsen
    Sep 17, 2004
  4. Bruce Sam
    Replies:
    15
    Views:
    7,850
    John C. Bollinger
    Nov 19, 2004
  5. Mark
    Replies:
    6
    Views:
    283
    Pramod Subramanyan
    Aug 24, 2005
Loading...

Share This Page