stringstream的妙用,在字符串中找出连续最长的数字串

在字符串中找出连续最长的数字串

  • stringstream的妙用

cin >> str;
str处理后 -> stringstream ss(str)
ss >> s

#include 
#include 
#include 
#include 


using namespace std;

int main()
{
    string str;
    while(cin >> str)
    {
        for(auto& c : str)
            if(!isdigit(c)) c = ' ';
        
        stringstream ss(str);
        int max_len = 0;
        string s, res;
        
        while(ss >> s)
        {
            if(max_len < s.size()){ 
                res  = s;
                max_len = s.size();
            }else if(max_len == s.size())
                 res += s;        
        }
        
        if(max_len != 0)
            cout << res << "," << max_len << endl;
        
    }
    
    return 0;
}

你可能感兴趣的:(stringstream的妙用,在字符串中找出连续最长的数字串)