<分区>
假设我们在 C++ 中有一个 C 风格的字符串,格式为 [4 letters] [number] [number] ...
。例如,字符串可能如下所示:
abcd 1234 -6242 1212
应该注意的是字符串应该有太多的空格(如上所示)。
我如何提取这三个数字并将它们存储在数组中?
<分区>
假设我们在 C++ 中有一个 C 风格的字符串,格式为 [4 letters] [number] [number] ...
。例如,字符串可能如下所示:
abcd 1234 -6242 1212
应该注意的是字符串应该有太多的空格(如上所示)。
我如何提取这三个数字并将它们存储在数组中?
最佳答案
stringstreams 的工作,实时查看: 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;
}
打印
abcd 1234 -6242 1212
关于C++ - 从字符串中提取数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17042710/