I wanted to know how I can grab numbers out of a string in C++. I'm getting the string from an input and will be getting multiple lines, but I already have the line reading working. Note: the lines always have an even number of ints
Here's what I'd like the code to look something like:
std::getline(std::cin, line);// line looks something like "10 3 40 45 8 12" int a, b; while(!line.empty() /*line still has ints to extract*/) { a = someMethod(line);//gets first int. So, 10 first loop, 40 second, 8 third b = someMethod(line);//gets second int. So, 3 first loop, 45 second, 12 third myMethod(a,b);//a method from elsewhere in my code. It's here so you know that I need both a and b } Anything similar would help. Thank you very much!
std::stringstream?lineand then use it in just the same way as you'd extract integers fromstd::cin. The library is nicely orthogonal this way. However, there are things to watch out for - see How to parse a string to an int in C++.