strtok() functionality in C++

Discussion in 'C++' started by Tydr Schnubbis, Nov 16, 2005.

  1. Is there a "C++ way" of doing what strtok() does, or should I just
    convert string objects into char arrays and use strtok()? I couldn't
    find a C++ way that is as straight forward as using strtok().

    Would the following be considered "good" C++ style?

    s = strtok(line.c_str(), " \t\n");
    while (s) {
    /* use s here */
    s = strtok(NULL, " \t\n");
    }
    Tydr Schnubbis, Nov 16, 2005
    #1
    1. Advertising

  2. Tydr Schnubbis

    Mike Wahler Guest

    "Tydr Schnubbis" <> wrote in message
    news:MoOef.7901$...
    > Is there a "C++ way" of doing what strtok() does,


    Yes. strtok() (It's as much a part of C++ as it is of C).

    > or should I just convert string objects into char arrays and use strtok()?
    > I couldn't find a C++ way that is as straight forward as using strtok().
    >
    > Would the following be considered "good" C++ style?
    >
    > s = strtok(line.c_str(), " \t\n");
    > while (s) {
    > /* use s here */
    > s = strtok(NULL, " \t\n");
    > }


    That depends entirely upon your needs.

    You can indeed tokenize a std::string object in various
    ways. One tool to that end is std::string::find_first_of()
    (which will locate a delimiter from a specified set,
    but doesn't replace it with a NULL as does strtok())

    -Mike
    Mike Wahler, Nov 16, 2005
    #2
    1. Advertising

  3. Tydr Schnubbis wrote:
    > Is there a "C++ way" of doing what strtok() does, or should I just
    > convert string objects into char arrays and use strtok()? I couldn't
    > find a C++ way that is as straight forward as using strtok().
    >
    > Would the following be considered "good" C++ style?
    >
    > s = strtok(line.c_str(), " \t\n");
    > while (s) {
    > /* use s here */
    > s = strtok(NULL, " \t\n");
    > }


    No it should not compile. strtok modifies it's argument and c_str()
    returns a const char*.

    There is no built in C++ string tokeniser. It's not too hard to write
    your own especially if you keep it simple, or you could get a third
    party one, for instance

    http://www.boost.org/libs/tokenizer/index.html

    john
    John Harrison, Nov 16, 2005
    #3
  4. Tydr Schnubbis

    Guest

    Tydr Schnubbis wrote:
    > Is there a "C++ way" of doing what strtok() does, or should I just
    > convert string objects into char arrays and use strtok()? I couldn't
    > find a C++ way that is as straight forward as using strtok().
    >
    > Would the following be considered "good" C++ style?
    >
    > s = strtok(line.c_str(), " \t\n");
    > while (s) {
    > /* use s here */
    > s = strtok(NULL, " \t\n");
    > }


    If whitespace is all you need as separators, then this should work for
    you:

    std::istringstream iss( line );
    std::string token;
    while( iss >> token )
    {
    // use 'token' here
    }
    , Nov 16, 2005
    #4
  5. wrote:
    > Tydr Schnubbis wrote:
    >> Is there a "C++ way" of doing what strtok() does, or should I just
    >> convert string objects into char arrays and use strtok()? I couldn't
    >> find a C++ way that is as straight forward as using strtok().
    >>
    >> Would the following be considered "good" C++ style?
    >>
    >> s = strtok(line.c_str(), " \t\n");
    >> while (s) {
    >> /* use s here */
    >> s = strtok(NULL, " \t\n");
    >> }

    >
    > If whitespace is all you need as separators, then this should work for
    > you:
    >
    > std::istringstream iss( line );
    > std::string token;
    > while( iss >> token )
    > {
    > // use 'token' here
    > }
    >

    Thanks, that was just what I was looking for. Simpler and cleaner than
    using strtok, as long as you don't require the extra flexibility.
    Tydr Schnubbis, Nov 17, 2005
    #5
  6. Tydr Schnubbis <> writes:

    > wrote:
    > > Tydr Schnubbis wrote:
    > >> Is there a "C++ way" of doing what strtok() does, or should I just
    > >> convert string objects into char arrays and use strtok()? I couldn't
    > >> find a C++ way that is as straight forward as using strtok().
    > >>
    > >> Would the following be considered "good" C++ style?
    > >>
    > >> s = strtok(line.c_str(), " \t\n");
    > >> while (s) {
    > >> /* use s here */
    > >> s = strtok(NULL, " \t\n");
    > >> }

    > > If whitespace is all you need as separators, then this should work
    > > for
    > > you:
    > > std::istringstream iss( line );
    > > std::string token;
    > > while( iss >> token )
    > > {
    > > // use 'token' here
    > > }
    > >

    > Thanks, that was just what I was looking for. Simpler and cleaner
    > than using strtok, as long as you don't require the extra flexibility.


    And if you do need the extra flexibility: (untested)

    string next_token(const string& line, const string& delim, string::size_type& first)
    {
    string::size_type last = line.find_first_of(delim, first);
    string next(line.substr(first, last));
    first = last;
    return next;
    }

    /Niklas Norrthon
    Niklas Norrthon, Nov 17, 2005
    #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. Adam Balgach
    Replies:
    2
    Views:
    557
    news-east
    Nov 28, 2004
  2. Alex Vinokur

    strtok() and std::string

    Alex Vinokur, Apr 14, 2005, in forum: C++
    Replies:
    6
    Views:
    4,895
    Pete Becker
    Apr 14, 2005
  3. strtok problem

    , Aug 28, 2003, in forum: C Programming
    Replies:
    4
    Views:
    498
  4. Robert

    strtok trouble

    Robert, Sep 5, 2003, in forum: C Programming
    Replies:
    17
    Views:
    1,212
    Jalapeno
    Sep 6, 2003
  5. Fatih Gey

    segfault on strtok

    Fatih Gey, Oct 23, 2003, in forum: C Programming
    Replies:
    40
    Views:
    1,437
    nobody
    Nov 1, 2003
Loading...

Share This Page