Paul said:
But is that the most efficient way?
However simplicty is sometime preferred over efficiency and the stream
method is quite handy, it is described here, bottom of page:
http://www.oopweb.com/CPP/Documents/CPPHOWTO/Volume/C++Programming-HOWTO-7.html
Is there a way to use this stream method when the delimiter is not
whitespace?
Yes, there's a technique called the "whitespace redefinition approach"
which will let you do that:
https://groups.google.com/group/comp.lang.c++/msg/4de0be2e4eb8e0ba?output=gplain&hl=de&pli=1
Personally I like the ability to use 'cin >> foo >> bar' style to read
delimited data from a stream, although most C++ programmers I know don't
really know/care much about locales, ctypes and facets, and
unfortunately I would expect they'd probably consider it to be "a bit
too weird" to use in their code.
Another one which I like, using TR1/Boost RegEx:
#include <string>
#include <regex>
#include <iostream>
int main()
{
std::string str = "the\t quick brown\n-\n- fox"
" jumped..over,the,lazy,.dog";
std::tr1::regex re("[\\s-,.]+");
std::tr1::sregex_token_iterator
iter(str.begin(), str.end(), re, -1),
end;
while(iter != end)
{
std::cout << *iter++ << std::endl;
}
}