Solved

Strings afterTokens in C++

Posted on 2009-05-09
3
212 Views
Last Modified: 2013-12-14
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
0
Comment
Question by:circler
3 Comments
 

Author Comment

by:circler
ID: 24346779
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

0
 
LVL 39

Accepted Solution

by:
itsmeandnobodyelse earned 500 total points
ID: 24350488
You simply take your own (?) Tokenize function and let it store the rest strings after token into a second vector.
void TokenizeAndAfterTokenize(const string& str,
                      vector<string>& tokens,
                      vector<string>& aftertokens,
                      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));
        // Push the rest - if any - to aftertokens
        if (pos != string::npos)
        {
            aftertokens.push_back(str.substr(pos));
        }
        // 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

0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Reference external folders in Visual Studio 4 84
teach junior high children .net programming 3 77
debug as  junit test 4 83
Tab order 16 79
The following diagram presents a diamond class hierarchy: As depicted, diamond inheritance denotes when two classes (e.g., CDerived1 and CDerived2), separately extending a common base class (e.g., CBase), are sub classed simultaneously by a fourt…
Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deleti…
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.

789 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question