Link to home
Start Free TrialLog in
Avatar of circler
circlerFlag for Lebanon

asked on

Strings afterTokens in C++

Hello,

I have a function that splits a string like this:
"abc def   ghij   "
into tokens "abc,def,ghij" and store each token in a vector<string>...

I need another function that takes two parameters
void my_func(std::string input , vector<string>& aftertoken)
this function must split a string into tokens
but instead of storing the token it must store string after tokens
so if "abc def   ghij   " is the input
output must be stored in a vector like:
aftertoken[0] = " def   ghij   "
aftertoken[1]=  "   ghij   "
aftertoken[2] = "   "

Note: I can't use c-string/ c functions only std
I hope I can get help soon... thanks
Avatar of circler
circler
Flag of Lebanon image

ASKER

Here is the Tokenize function:

void Tokenize(const string& str,
                      vector<string>& tokens,
                      const string& delimiters)
{
    // Skip delimiters at beginning.
    string::size_type lastPos = str.find_first_not_of(delimiters, 0);
    // Find first "non-delimiter".
    string::size_type pos     = str.find_first_of(delimiters, lastPos);
 
    while (string::npos != pos || string::npos != lastPos)
    {
        // Found a token, add it to the vector.
        tokens.push_back(str.substr(lastPos, pos - lastPos));
        // Skip delimiters.  Note the "not_of"
        lastPos = str.find_first_not_of(delimiters, pos);
        // Find next "non-delimiter"
        pos = str.find_first_of(delimiters, lastPos);
    }
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of itsmeandnobodyelse
itsmeandnobodyelse
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial