C++中的字符串流,用于解析字符串和数字

TJa*_*ain 5 c++ string stringstream

我有这样的字符串:'123plus43times7'

其中数字后跟字典中的单词.

我知道我可以使用>>运算符提取int/numbers :

StringStream >> number
Run Code Online (Sandbox Code Playgroud)

我可以得到这个号码.但是,Stream仍然有数字.如果数字长度未知或者我应该找出数字的长度,然后使用str.substr()创建新的字符串流,如何删除该数字?使用C++ STL String和SStream执行此任何其他更好的方法将非常感激.

Bar*_*ani 5

您可以在文本和数字之间插入空格,然后使用 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;
}
Run Code Online (Sandbox Code Playgroud)