Lecture Notes for CS 509, Advanced Programming II

31 October 2002, Splitting Strings


Koenig and Moo Split a String

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

strvec
split(const std::string & str) 

  strvec words
  str_indx b = 0

  while b != str.size()
    while (b != str.size()) and std::isspace(str[b])
      ++b

    str_indx e = b
    while (e != str.size()) and !std::isspace(str[e])
      ++e

    if b != e
      words.push_back(str.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 5 November 2002.