C++中实现字符串函数split
1 int split(const string &str, vector<std::string> &des, char delim) { 2 char str_tmp[str.size() + 1]; 3 snprintf(str_tmp, sizeof(char) * (str.size() + 1), str.c_str()); 4 while(1) { 5 int pos = str.find(delim); 6 if(pos == 0) { 7 str = str.substr(1); 8 continue; 9 } 10 if(pos<0) { 11 des.push_back(str); 12 break; 13 } 14 string tmp = str.substr(0, pos); 15 str = str.substr(pos+1); 16 des.push_back(tmp); 17 } 18 19 return des; 20 }

浙公网安备 33010602011771号