我有这样的字符串:
'123加43次7'
其中数字后面跟着字典中的单词。
我知道我可以使用以下命令提取 int/numbers>>
操作员:
StringStream >> number
我可以拿到号码。然而,Stream 中仍然有该号码。当数字长度未知时如何删除数字,或者我应该找出数字长度然后使用 str.substr() 创建新的 String Stream ?
如果有任何其他更好的方法使用 C++ STL String 和 SStream 来完成此操作,我们将不胜感激。
您可以在文本和数字之间插入空格,然后使用std::stringstream
#include <iostream>
#include <string>
#include <sstream>
#include <cctype>
int main()
{
std::string s = "123plus43times7";
for (size_t i = 0; i < (s.size() -1 ); i++)
{
if (std::isalpha(s[i]) != std::isalpha(s[i + 1]))
{
i++;
s.insert(i, " ");
}
}
std::stringstream ss(s);
while (ss >> s)
std::cout << s << "\n";
return 0;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)