C ++ - print numbers from a string

Let's say we have a C-style string in C ++ in the format [4 letters] [number] [number] ... For example, a line might look like this:

  abcd 1234 -6242 1212 

It should be noted that there are expected to be too many spaces in the line (as shown above).

How can I extract these three numbers and store them in an array?

+6
source share
1 answer

Task for the lines, see it live: http://ideone.com/e8GjMg

 #include <sstream> #include <iostream> int main() { std::istringstream iss(" abcd 1234 -6242 1212"); std::string s; int a, b, c; iss >> s >> a >> b >> c; std::cout << s << " " << a << " " << b << " " << c << std::endl; } 

Print

 abcd 1234 -6242 1212 
+11
source

Source: https://habr.com/ru/post/947022/


All Articles