CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums
Results 1 to 3 of 3

Thread: split string function

  1. #1
    Join Date
    May 2015

    split string function


    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 :

    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] == ' ') {
        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 ?!


  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012

    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 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++17 Compiler: Microsoft VS2019 (16.7.7)

  3. #3
    Join Date
    May 2015

    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

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)