Use Boost's string algorithm library to split
the string into a vector of strings, then std::for_each
and either atoi
or boost::lexical_cast
to turn them into int
s. It's likely to be far simpler than other methods, but may not have the greatest performance due to the copy (if someone has a way to improve it and remove that, please comment).
std::vector<int> numbers;
void append(std::string part)
{
numbers.push_back(boost::lexical_cast<int>(part));
}
std::string line = "42 4711"; // borrowed from sbi's answer
std::vector<std::string> parts;
split(parts, line, is_any_of(" ,;"));
std::for_each(parts.being(), parts.end(), append);
Roughly.
http://www.boost.org/doc/libs/1_44_0/doc/html/string_algo.html
http://www.boost.org/doc/libs/1_44_0/libs/conversion/lexical_cast.htm