 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Kelvin@!!! Guest
|
Posted: Thu Feb 24, 2005 6:24 am Post subject: tokenize a string |
|
|
hi:
in C, we can use strtok() to tokenize a char*
but i can't find any similar member function of string that can tokenize a
string
so how so i tokenize a string in C++?
do it the C way?
thanks
--
{ Kelvin@!!! }
remove the last .hk to reply
thanks
|
|
| Back to top |
|
 |
red floyd Guest
|
Posted: Thu Feb 24, 2005 7:34 am Post subject: Re: tokenize a string |
|
|
Kelvin@!!! wrote:
| Quote: | hi:
in C, we can use strtok() to tokenize a char*
but i can't find any similar member function of string that can tokenize a
string
so how so i tokenize a string in C++?
do it the C way?
thanks
|
Look up std::istringstream in your favorite reference book.
|
|
| Back to top |
|
 |
ulrich Guest
|
Posted: Thu Feb 24, 2005 7:44 am Post subject: Re: tokenize a string |
|
|
On Thu, 24 Feb 2005 06:24:31 GMT, Kelvin@!!!
<chickenwing604-skool (AT) yahoo (DOT) com.hk.hk> wrote:
| Quote: | hi:
in C, we can use strtok() to tokenize a char*
but i can't find any similar member function of string that can tokenize
a
string
so how so i tokenize a string in C++?
|
you may want to try boost::tokenizer an relatives.
http://www.boost.org/libs/tokenizer/index.html
|
|
| Back to top |
|
 |
rossum Guest
|
Posted: Thu Feb 24, 2005 11:14 pm Post subject: Re: tokenize a string |
|
|
On Thu, 24 Feb 2005 06:24:31 GMT, "Kelvin@!!!"
<chickenwing604-skool (AT) yahoo (DOT) com.hk.hk> wrote:
| Quote: | hi:
in C, we can use strtok() to tokenize a char*
but i can't find any similar member function of string that can tokenize a
string
so how so i tokenize a string in C++?
do it the C way?
thanks
|
There is a sample chapter from Accelerated C++ on the web at
http://www.awprofessional.com/articles/article.asp?p=25333
The chapter has a function called split() which does what you seem to
want, it takes a string and returns a vector of all the individual
words:
// true if the argument is whitespace, false otherwise
bool space(char c) { return isspace(c); }
// false if the argument is whitespace, true otherwise
bool not_space(char c) { return !isspace(c); }
vector<string> split(const string& str) {
typedef string::const_iterator iter;
vector<string> ret;
iter i = str.begin();
while (i != str.end()) {
// ignore leading blanks
i = find_if(i, str.end(), not_space);
// find end of next word
iter j = find_if(i, str.end(), space);
// copy the characters in [i, j)
if (i != str.end()) ret.push_back(string(i, j));
i = j;
}
return ret;
}
There is a detailed explanation of the functino in the text.
rossum
--
The ultimate truth is that there is no Ultimate Truth
|
|
| Back to top |
|
 |
davidrubin@warpmail.net Guest
|
Posted: Fri Feb 25, 2005 2:59 am Post subject: Re: tokenize a string |
|
|
rossum wrote:
| Quote: | // true if the argument is whitespace, false otherwise
bool space(char c) { return isspace(c); }
// false if the argument is whitespace, true otherwise
bool not_space(char c) { return !isspace(c); }
vector<string> split(const string& str) {
typedef string::const_iterator iter;
vector<string> ret;
iter i = str.begin();
while (i != str.end()) {
// ignore leading blanks
i = find_if(i, str.end(), not_space);
// find end of next word
iter j = find_if(i, str.end(), space);
// copy the characters in [i, j)
if (i != str.end()) ret.push_back(string(i, j));
i = j;
}
return ret;
}
|
This would be better if it was templatized by an insertion iterator
rather than returning a vector by value. Something along the lines of
(untested)
template <typename InsertIter>
int
tokenize(const std::string& buf,
const std::string& delims,
InsertIter it)
{
std::string::size_type sp; // start position
std::string::size_type ep; // end position
int numTokens = 0;
do {
sp = buf.find_first_not_of(delims, sp);
ep = buf.find_first_of(delims, sp);
if (sp != ep) {
if (ep == buf.npos) {
ep = buf.length();
}
*it++ = buf.substr(sp, ep - sp);
++numTokens;
sp = buf.find_first_not_of(delims, ep + 1);
}
} while (sp != buf.npos);
if (sp != buf.npos) {
*it++ = buf.substr(sp, buf.length() - sp);
++numTokens;
}
return numTokens;
}
called as
std::deque<std::string> tokens;
int numTokens = tokenize(buf, delims, std::back_inserter(tokens));
/david
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|