Solved

Strings afterTokens in C++

Posted on 2009-05-09
3
201 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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

In our object-oriented world the class is a minimal unit, a brick for constructing our applications. It is an abstraction and we know well how to use it. In well-designed software we are not usually interested in knowing how objects look in memory. …
In Easy String Encryption Using CryptoAPI in C++ (http://www.experts-exchange.com/viewArticle.jsp?aid=1193) I described how to encrypt text and recommended that the encrypted text be stored as a series of hexadecimal digits -- because cyphertext may…
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 new code templates in NetBeans IDE 8.0 for Windows.

809 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