识别输入流中有几个整数

一行中有不超过五个证书(如果超过五个 则识别前五个)从左到右识别 知道末尾或遇到非数字字符 输出整数个数 若没有数据 输出-1

img


#include <iostream>
#include <vector>
#include <fstream>
#include <sstream>
#include <algorithm>

using namespace std;

int main() {
    ifstream data("../data.txt"); //待读取文件的目录
    vector<int> res;
    string line;
    while (getline(data, line)) {
        stringstream ss; //输入流
        ss << line; //向流中传值
        if (!ss.eof()) {
            int temp;
            while (ss >> temp) //提取int数据
                res.push_back(temp); //保存到vector
        }
    }
    sort(res.begin(), res.end());
    cout << res.back() << " " << res.front() << endl;
    return 0;
}