ACM常见格式提取代码
1. 读入数字后再读入含空格的字符串
int n; cin>>n;
cin.ignore();
vector<string> strs(n);
for(int i=0;i<n;i++)
getline(cin,strs[i])
2. 读入未知长度的数字
vector<int> nums;
int a;
while(cin>>a){
nums.push_back(a);
if(cin.get()=='\n') break;
}
3. 提取.划分的字符串
vector<string> tokens;
istringstream iss(input);
string token;
while (getline(iss, token, '.'))
tokens.push_back(token);
4. 提取指定范围符号内用逗号隔开数字
vector<int> trans(string &str){
size_t start = str.find('[');
size_t end = str.find(']');
string numStr = str.substr(start + 1, end - start - 1);
stringstream ss(numStr);
int number;
vector<int> numbers;
while (ss >> number) {
numbers.push_back(number); // 将提取到的数字添加至 vector 容器中
if (ss.peek() == ',')
ss.ignore(); // 忽略逗号分隔符
}
return numbers;
}
5. 提取字符串内的24时制时间
转化为分钟
int time_trans(string time){
stringstream ss(time);
int hours, minutes;
char delimiter;
ss >> hours >> delimiter >>noskipws >> minutes;
return hours*60 + minutes;
}
6. 正则表达式提取日期字符串
regex pattern("\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}");
string feature(string &s){
smatch matches;
regex_search(s, matches, pattern);
return matches[0];
}