Lecture Notes for CS 509, Advanced Programming II

8 April 2003, Splitting Strings


Koenig and Moo Split a String

typedef std::vector strvec
typedef std::string::size_type   str_indx

strvec
split(const std::string & s) 

  strvec words
  str_indx b = 0

  while b != s.size()
    while (b != s.size()) and isspace(s[b])
      ++b

    str_indx e = b
    while (e != s.size()) and !isspace(s[e])
      ++e

    if b != e
      words.push_back(s.substr(b, e - b))
      b = e
      
  return words


Know Your Tools


Being Generic


Loops are Evil


Prefer Member Functions


What's This Going to Cost?

string splitting timings


Points to Remember


This page last modified on 11 April 2003.