CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    May 2015
    Posts
    500

    split string function

    Hi,

    I was trying to practice my coding , and found the following code for splitting string and finally use those to convert to integer and get sum of those numbers.

    I found the split string is written is complicated way :

    Code:
    vector<string> split_string(string input_string) {
        string::iterator new_end = unique(input_string.begin(), input_string.end(), [] (const char &x, const char &y) {
            return x == y and x == ' ';
        });
    
        input_string.erase(new_end, input_string.end());
    
        while (input_string[input_string.length() - 1] == ' ') {
            input_string.pop_back();
        }
    
        vector<string> splits;
        char delimiter = ' ';
    
        size_t i = 0;
        size_t pos = input_string.find(delimiter);
    
        while (pos != string::npos) {
            splits.push_back(input_string.substr(i, pos - i));
    
            i = pos + 1;
            pos = input_string.find(delimiter, i);
        }
    
        splits.push_back(input_string.substr(i, min(pos, input_string.length()) - i + 1));
    
        return splits;
    }
    not sure why the" return x == y and x == ' '; " is needed. May be i am not much into stl strings and i need to dig a bit deeper here ?!

    thanks
    pdk

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: split string function

    string_split is splitting a string at ' ' into a vector. The marked code removes duplicate spaces (makes multiple ones into just 1). It uses the std:: duplicate() function with a lambda. The result of the lambda informs duplicate() whether to remove an element or not. The result is only true if both x and y are ' ' ie adjacent chars are both ' '.

    It next removes trailing spaces. Then iterates the string to split into substrings to store in the vector. The code's not that great. Don't think it's the best way of doing it.
    Last edited by 2kaud; September 19th, 2020 at 01:40 PM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    Join Date
    May 2015
    Posts
    500

    Re: split string function

    Thanks a lot kaud

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