Lecture Notes for CS 509, Advanced Programming II

30 March 2004, 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 25 March 2004.