splitting a string

Started by
4 comments, last by logout 20 years, 1 month ago
I got a line like this: "this is a line of words that i want to do somthing with" Now I want to convert this into a vector. where each element represent a word.: myVector[0] = "this"; myVector[1] = "is"; myVector[2] = "a"; myVector[3] = "line"; myVector[4] = "of"; ... .. Now I figrued this can be done using stringstreams but im not sure on how i would go about that. Specaly the part of getting the stuff from the SS to the vector. Help plz
Advertisement
a) Use a tokenizer, such as boost::tokenizer - that's what they are designed for.

b) Use stream iterators:
#include <vector>#include <sstream>#include <iterator>#include <iostream>#include <algorithm>using namespace std;vector<string> split(const string& str){   stringstream ss(str);   return vector<string>( istream_iterator<string>(ss),                          istream_iterator<string>() );}int main(){   string str = "this is a line of words that i want to do somthing with";   vector<string> v = split(str);   copy( v.begin(), v.end(), ostream_iterator<string>(cout, "\n"));   return 0;}


istream_iterator<string>(ss) returns an iterator to the beginning of the ss stream, and treats it as a sequence of string. And the default way strings are read, is one whitespace-separated 'word' at a time.

istream_iterator<string>() returns an "end of stream" iterator matching the one above.

ostream_iterator<string>(cout, "\n") works similary with output streams, here providing an output iterator, as required by the copy algorithm.

With these iterator adapters, you can use streams as sequences, in any algorithm that uses Input or Output iterators (as opposed to Forward/Bidirectional/Random iterators).

“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.” — Brian W. Kernighan (C programming language co-inventor)

[edited by - Fruny on February 28, 2004 1:59:11 PM]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
If you don''t grok stream iterators, a different approach would be:
  std::stringstream sstr("this is a line of words that i want to do somthing with");  std::vector<std::string> words;  while (sstr.good()) {    std::string temp;    sstr >> temp;    words.push_back(temp);  }


There''s also the difficult to use and error prone strtok() function in the standard C library.
Just out of curiosity, whats a grok?
quote:Original post by Charleh
Just out of curiosity, whats a grok?


Jargon File

grok: /grok/, /grohk/, vt

[common; from the novel Stranger in a Strange Land, by Robert A. Heinlein, where it is a Martian word meaning literally ‘to drink’ and metaphorically ‘to be one with’] The emphatic form is grok in fullness.

1. To understand. Connotes intimate and exhaustive knowledge. When you claim to ‘grok’ some knowledge or technique, you are asserting that you have not merely learned it in a detached instrumental way but that it has become part of you, part of your identity. For example, to say that you “know” LISP is simply to assert that you can code in it if necessary — but to say you “grok” LISP is to claim that you have deeply entered the world-view and spirit of the language, with the implication that it has transformed your view of programming. Contrast zen, which is similar supernal understanding experienced as a single brief flash. See also glark.

2. Used of programs, may connote merely sufficient understanding. “Almost all C compilers grok the void type these days.”

“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.” — Brian W. Kernighan (C programming language co-inventor)

[edited by - Fruny on February 28, 2004 2:13:11 PM]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Fruny:
option b) was the one I was trying to do..
thx;

oh that grok thing was pretty cool ...

This topic is closed to new replies.

Advertisement