Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. The Lounge
  3. Question of the day....

Question of the day....

Scheduled Pinned Locked Moved The Lounge
csharpquestionc++css
22 Posts 8 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Michael Dunn

    Chris Maunder wrote: So send me some STL questions then. Q: How would you write a strtok() equivalent using the string 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_Helm

    T Offline
    T Offline
    Thomas Freudenberg
    wrote on last edited by
    #21

    Following 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.

    1 Reply Last reply
    0
    • C Christian Graus

      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

      J Offline
      J Offline
      jkgh
      wrote on last edited by
      #22

      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".

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups