If you are averse to boost, you can use regular old operator>>
, along with std::noskipws
:
EDIT: updates after testing.
#include
#include
#include
#include
#include
#include
#include
void split(const std::string& str, std::vector& v) {
std::stringstream ss(str);
ss >> std::noskipws;
std::string field;
char ws_delim;
while(1) {
if( ss >> field )
v.push_back(field);
else if (ss.eof())
break;
else
v.push_back(std::string());
ss.clear();
ss >> ws_delim;
}
}
int main() {
std::vector v;
split("hello world how are you", v);
std::copy(v.begin(), v.end(), std::ostream_iterator(std::cout, "-"));
std::cout << "\n";
}
http://ideone.com/62McC