How do you get a std::stringstream
to return the next token, instead of extracting it into a variable with operator>>
?
I've included some examples of why I would want to do this below. I've tried get()
, but this just returns the next character.
#include <string>
#include <sstream>
std::string line("abc xyz");
std::stringstring ss(line);
std::string token;
ss >> token; // I don't want this
token = ss.next(); // I want something like this
if (ss.next() == "abc") // So I can do something like this
int x = std::stoi(ss.next()); // Or this
The main reason for this is to produce cleaner code when I don't want to store or reuse the next token. Otherwise, the code is filled with lines ss >> token
.