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; }
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报