CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 24 of 24
  1. #16
    Join Date
    Jun 2008
    Posts
    592

    Re: splitting a string

    lol I can get it down to 3 lines inside a function, but no one liner . Perhaps 2
    0100 0111 0110 1111 0110 0100 0010 0000 0110 1001 0111 0011 0010 0000 0110 0110 0110 1111 0111 0010
    0110 0101 0111 0110 0110 0101 0111 0010 0010 0001 0010 0001 0000 0000 0000 0000
    0000 0000 0000 0000

  2. #17
    Join Date
    Jan 2008
    Location
    California, USA
    Posts
    822

    Re: splitting a string

    I should've said "a single line of statement" instead of just saying a single line.
    My one liner spans across the screen little too much
    So I'm sure your 2 lines is as good as mine, if not better.

  3. #18
    Join Date
    Jun 2008
    Posts
    592

    Re: splitting a string

    lol here is my code

    Code:
    void Tokenize( const string& TokenString, vector< string >& Tokens, char Delimiter = '$' )
    {
        string Token;
        stringstream TokenSplitter( TokenString );
        while( getline( TokenSplitter, Token, Delimiter ) ) Tokens.push_back( Token );
    }
    and this is how you use it
    Code:
    vector<string> Tokens;
    Tokenize( "1 2$3$4\n5$6", Tokens );
    I think that is 3 statements enjoy
    0100 0111 0110 1111 0110 0100 0010 0000 0110 1001 0111 0011 0010 0000 0110 0110 0110 1111 0111 0010
    0110 0101 0111 0110 0110 0101 0111 0010 0010 0001 0010 0001 0000 0000 0000 0000
    0000 0000 0000 0000

  4. #19
    Join Date
    Jan 2008
    Location
    California, USA
    Posts
    822

    Re: splitting a string

    Quote Originally Posted by Joeman View Post
    I think that is 3 statements enjoy
    That's great
    Mine can be used if there's no luxury of affording 3 statements
    Code:
    void Chunkify(const string& s, vector<string>& v, unsigned int p = 0, char delim = '$')
    {
        return string::npos != (p = s.find(delim)) ? v.push_back(s.substr(0, p)), Chunkify(s.substr(p + 1), v, p) : v.push_back(s);
    }
    and can be used with a similar function call as yours
    Code:
    vector<string> Tokens;
    Chunkify( "1 2$3$4\n5$6", Tokens );
    But as I said before, I'm sure yours is better

  5. #20
    Join Date
    Jun 2008
    Posts
    592

    Re: splitting a string

    lol nice one That is some way of doing it

    I suppose you can take out that return keyword in your function?
    Last edited by Joeman; January 30th, 2010 at 01:56 AM.
    0100 0111 0110 1111 0110 0100 0010 0000 0110 1001 0111 0011 0010 0000 0110 0110 0110 1111 0111 0010
    0110 0101 0111 0110 0110 0101 0111 0010 0010 0001 0010 0001 0000 0000 0000 0000
    0000 0000 0000 0000

  6. #21
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: splitting a string

    Quote Originally Posted by Joeman
    I think that is 3 statements
    Four, depending on your point of view. But you probably should have let farooq124in try it first.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  7. #22
    Join Date
    Jun 2008
    Posts
    592

    Re: splitting a string

    I am not complaining since farooq124in did try enough and it is only so little of code. It isn't 100 lines of unique code

    You will notice I didn't include headers for a reason
    0100 0111 0110 1111 0110 0100 0010 0000 0110 1001 0111 0011 0010 0000 0110 0110 0110 1111 0111 0010
    0110 0101 0111 0110 0110 0101 0111 0010 0010 0001 0010 0001 0000 0000 0000 0000
    0000 0000 0000 0000

  8. #23
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: splitting a string

    Quote Originally Posted by Joeman
    You will notice I didn't include headers for a reason
    Heheh
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  9. #24
    Join Date
    Jan 2008
    Location
    California, USA
    Posts
    822

    Re: splitting a string

    Quote Originally Posted by Joeman View Post
    I suppose you can take out that return keyword in your function?
    Haha of course! 98% of the time!
    I just left it there to help improve what little purpose there is to indicate that the function is recursive

Page 2 of 2 FirstFirst 12

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured