1 vector<string> split(const string& src, const string& sep) { 2 vector<string> tokens; 3 int lastPos = 0, // 上次找到的sep的位置 4 index, 5 sepLen = sep.length(); 6 while ( -1 != (index = src.find(sep, lastPos) )) { 7 // substr(起始位置,字符串长度) 8 tokens.push_back(src.substr(lastPos, index - lastPos)); 9 lastPos = index + sepLen; 10 } 11 12 string lastString = src.substr(lastPos); 13 if (!lastString.empty()) { 14 tokens.push_back(lastString); 15 } 16 17 return tokens; 18 19 }