Solved

Strings afterTokens in C++

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.

708 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now