niXforums Forum Index
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   PreferencesPreferences   Log in to check your private messagesLog in to check your private messages   Log inLog in 
·  nixdoc.net ·  man pages ·  Linux HOWTOs ·  FreeBSD Tips ·  Forums
navigation Forum index » Programming » C++
Help with Tokenizer Please
Post new topic   Reply to topic Page 1 of 1 [2 Posts] View previous topic :: View next topic
Author Message
Victor Bazarov
*nix forums Guru


Joined: 07 Apr 2005
Posts: 3949

PostPosted: Sun Feb 26, 2006 8:01 pm    Post subject: Re: Help with Tokenizer Please Reply with quote

electrixnow wrote:
Quote:
I use a code snippet called tokenize that works quite well. But I need
to modify it so it returns an empty string if two commas are found
together: ie. string test="1,2,,4,5"

I would like to have

tokens[0] = "1"
tokens[1] = "2"
tokens[3] = ""
tokens[4] = "4"
tokens[5] = "5"

This would allow me to find missing tokens. The following is the code
I use.
Can anyone show me the modification I would need?
-----------------

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

I think you shouldn't skip delimiters here. You just need to make
'lastPos' to be the same as 'pos'. Replace the statement above with

lastPos = pos;

Quote:
pos = str.find_first_of(delimiters, lastPos);
// Find next "non-delimiter"
}
}

--------------

I didn't check it. It's just a hunch.

V
--
Please remove capital As from my address when replying by mail
Back to top
electrixnow
*nix forums beginner


Joined: 05 Feb 2006
Posts: 24

PostPosted: Sun Feb 26, 2006 7:46 pm    Post subject: Help with Tokenizer Please Reply with quote

I use a code snippet called tokenize that works quite well. But I need
to modify it so it returns an empty string if two commas are found
together: ie. string test="1,2,,4,5"

I would like to have

tokens[0] = "1"
tokens[1] = "2"
tokens[3] = ""
tokens[4] = "4"
tokens[5] = "5"

This would allow me to find missing tokens. The following is the code I
use.
Can anyone show me the modification I would need?
-----------------

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

--------------

Thanks in advance,
Grant
Back to top
Google

Back to top
Display posts from previous:   
Post new topic   Reply to topic Page 1 of 1 [2 Posts] View previous topic :: View next topic
The time now is Thu Jan 08, 2009 12:25 pm | All times are GMT
navigation Forum index » Programming » C++
Jump to:  

Similar Topics
Topic Author Forum Replies Last Post
No new posts Tokenizer Bit byte C++ 2 Fri Jul 21, 2006 2:40 am
No new posts Tokenizer Function (plus rant on strtok documentation) Robbie Hatley C++ 18 Tue Jul 11, 2006 3:09 am
No new posts tokenizer compboy C 6 Tue May 02, 2006 3:34 pm
No new posts Issues with npos? writing a non-boost string tokenizer JohnFlyTn@msn.com C++ 1 Thu Feb 16, 2006 4:33 pm
No new posts readline tokenizer newline sticky wicket Arthur python 0 Tue Feb 07, 2006 2:55 am

Web Advertising | Dutch Bodybuilding Forums | Guitar Lesson | Payday Loans | CitiBank Credit Cards
Copyright © 2004-2005 DeniX Solutions SRL
 
Other DeniX Solutions sites: Unix/Linux blog |  electronics forum |  medicine forum |  science forum | 
Privacy Policy


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.1927s ][ Queries: 20 (0.1065s) ][ GZIP on - Debug on ]