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

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

  • stringstream的妙用

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

#include <iostream>
#include <algorithm>
#include <cstring>
#include <sstream>


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;
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容