#include <iostream>
#include <vector>
#include <sstream>
std::vector<int> read_integers(std::istream & input)
{
std::vector<int> numbers;
int number = 0;
bool have_number = false;
char c;
// Loop until reading fails.
while (input.get(c)) {
if (c >= '0' && c <= '9') {
// We have a digit.
have_number = true;
// Add the digit to the right of our number. (No overflow check here!)
number = number * 10 + (c - '0');
} else if (have_number) {
// It wasn't a digit and we started on a number, so we hit the end of it.
numbers.push_back(number);
have_number = false;
number = 0;
}
}
// Make sure if we ended with a number that we return it, too.
if (have_number) { numbers.push_back(number); }
return numbers;
}
int main() {
auto const input_string = std::string("12f 356 48 r56 fs6879 57g 132e efw ddf312 323f");
std::istringstream input_stream(input_string);
std::cout << "Input: " << input_string << std::endl;
auto ints = read_integers(input_stream);
std::cout << "Output:" << std::endl;
for (auto i = ints.begin(); i != ints.end(); ++i) {
std::cout << "* " << *i << std::endl;
}
return 0;
}