1. string split
通常会遇到处理字符串分割步骤,按照分隔符是单字符or多个分割符
单字符分割
void split(string& input, vector<string>& output, const char& delim){
istringstream ss(input);
string tmp;
while(getline(ss, tmp, delim)){
if(tmp.size() == 0) continue;
output.push_back(tmp);
}
return ;
}
多字符分割
void split(string& input, vector<string>& output, const string& delim){
char* s = new char[input.size() + 1];
memset(s, 0, input.size() + 1);
strcpy(strc, str.c_str());
char* tmpStr = strtok(s, delim.c_str());
while(tmpStr != NULL){
output.push_back(string(tmpStr));
tmpStr = strtok(NULL, delim.c_str());
}
delete [] s;
return ;
}