其中1 2 3 的长度不定,
使用正则表达式进行匹配,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;
}
这个文件中的-a,-b,-c后面能够确定都是数字吗?然后将-a,-b,-c后面的数字取出来? 你说的“其中1 2 3 长度不变”是什么意思?