自己记录作用,以便以后查阅,很简单的小程序
具体代码如下
参数 Input 代表被分割的字符串,参数delimiter代表分隔符
vector<string> VSplit(const string& input, const string& delimiter) {
vector<string> temp;
int start = 0;
int pos = 0;
while (input.find_first_of(delimiter, start) != string::npos) {
pos = input.find_first_of(delimiter, start);
temp.emplace_back(input, start, pos - start);
start = pos + 1;
}
if (start < input.length()) {
temp.emplace_back(input, start);
}
return temp;
}
std::vector<std::string> Split(std::string& s, char del) {
int start = 0;
int pos = 0;
int l = s.length();
std::vector<std::string> res;
while (pos < l) {
while (s[pos] == del && pos < l) {
pos++;
}
start = pos;
while (s[pos] != del && pos < l) {
pos++;
}
if (start < l) {
res.push_back(s.substr(start, pos - start));
}
}
return res;
}