C++ 版本的split_string

时间:2023-03-09 23:45:55
C++ 版本的split_string
vector<string> split_string(const string &in, char del, bool skip_empty) {
vector<string> res; if (in.empty() || del == '\0')
return res; string field;
istringstream f(in);
if (del == '\n') {
while(getline(f, field)) {
if (field.empty() && skip_empty)
continue;
res.push_back(field);
}
} else {
while(getline(f, field, del)) {
if (field.empty() && skip_empty)
continue;
res.push_back(field);
}
}
return res;
}