Question of the day....
-
Chris Maunder wrote: So send me some STL questions then. Q: How would you write a
strtok()
equivalent using thestring
methods? Mail your responses to Mike Dunn (preferably by tomorrow, not that he has a deadline then or anything). --Mike-- Just released - RightClick-Encrypt - Adds fast & easy file encryption to Explorer Like the Google toolbar? Then check out UltraBar, with more features & customizable search engines! My really out-of-date homepage Sonork-100.19012 Acid_HelmFollowing code I am using. It's a modificated version of http://gcc.gnu.org/onlinedocs/libstdc++/21_strings/stringtok_std_h.txt [Paul]
template <typename outit>
void tokenize (outit x, const std::tstring &in, const TCHAR * delimiters = _T(" \t\n"))
{
const std::tstring::size_type len = in.length();
std::tstring::size_type i = 0;while ( i < len ) { // eat leading whitespace i = in.find\_first\_not\_of (delimiters, i); if (i == std::tstring::npos) return; // nothing left but white space // find the end of the token std::tstring::size\_type j = in.find\_first\_of (delimiters, i); // push token if (j == std::tstring::npos) { \*x = in.substr(i); ++x; return; } else { \*x = in.substr(i, j-i); ++x; } // set up for next loop i = j + 1; }
}
Example:
std::liststd::string ls;
utl::tokenize (std::back_inserter(ls), " this \t is\t\n a test ");
for (std::list::const_iterator it = ls.begin(); it != ls.end(); ++it)
{
std::cerr << ':' << (*it) << ":\n";
}would produce following output:
:this:
:is:
:a:
:test:Regards Thomas Sonork id: 100.10453 Thömmi
Disclaimer:
Because of heavy processing requirements, we are currently using some of your unused brain capacity for backup processing. Please ignore any hallucinations, voices or unusual dreams you may experience. Please avoid concentration-intensive tasks until further notice. Thank you. -
Why am I being asked about VB.NET operators on a C++ programming website ? I wouldn't mind if it was C#, but surely only a very small number of us are afflicted with VB, and VB.NET even less so ? Christian I am completely intolerant of stupidity. Stupidity is, of course, anything that doesn't conform to my way of thinking. - Jamie Hale - 29/05/2002
Do you fancy coming up with a few lint-type questions? [what do you do if you have an inny type button] Alice thought that running very fast for a long time would get you to somewhere else. " A very slow kind of country!" said the queen. "Now, here , you see, it takes all the running you can do, to keep in the same place".