P_zz 2018-11-13 03:16 采纳率: 54.5%
浏览 955
已采纳

如何提取特定符号后的数据

图片说明

就是将图片中 -a -b -c 后面的1 2 3 读取出来

            其中1 2 3 的长度不定,
  • 写回答

3条回答 默认 最新

  • Italink 2018-11-13 04:46
    关注

    使用正则表达式进行匹配,LZ可以看一下正则表达式的语法

     #include<iostream>
    #include<fstream>
    #include<string>
    #include<regex>
    using namespace std;
    int main() {
        string str;
        int count = 0;
        ifstream in("in.txt");
        getline(in, str);       //读取第一行文本
        int result[1000];
        regex pat("-\\w{1,} (\\d{1,})");  //匹配模式
        smatch matches;                 //存储匹配到的字符串
        while (1) {
            if (!regex_search(str, matches, pat))   
                break;
            result[count++] = stoi(matches[1]);
            str=matches.suffix();       //修改剩余的字符串
        }
        for (int i = 0; i < count; i++) {
            cout << result[i] << " ";
        }
        return 0;
    }
    

    图片说明

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?