自己写了个字符串分割函数,根据用户的输入确认分割字符,用户输入的分割字符必须为英文字符,否则会出错。
为了解析XML,我真是要把JAVA那套机制给搬过来了,前几天还在琢磨反射,也就是将XML中的数据映射到C++的类中。
#include#include #include using namespace std; vector split(string str, const char* szChar) { int pos = str.find(szChar, 0); int first = 0; vector temp; if (-1 == pos )//找不到分割的字符 { temp.push_back(str); return temp; //原路返回 } while (pos != -1) { temp.push_back(str.substr(first,pos-first)); pos += 1; first = pos; pos = str.find(szChar,pos); } if (first != str.size() - 1) { temp.push_back(str.substr(first, str.size() - first)); } return temp; } int main() { string str("kkk,Tom,GIRRR,BOY,GIRIL,TIM"); string str2("你好,再见"); vector out; out = split(str2, ","); for (int i = 0; i < out.size(); i++) cout << out.at(i) << endl; return 0; }



