std::string字符串分割

来源:互联网 发布:qq群倍投软件 编辑:程序博客网 时间:2024/04/30 12:19
废话不多说,直接上代码:
std::vector<std::string> split(std::string str, std::string pat){std::vector<std::string> bufStr;while (true){int index = str.find(pat);std::string subStr = str.substr(0, index);if (!subStr.empty())bufStr.push_back(subStr);str.erase(0, index + pat.size());if (index == -1)break;}return bufStr;}
使用方式:
std::vector<std::string> plits = split("192.168.0.1", ".");for (int i = 0; i < plits.size(); i++){printf("###%s###", plits.at(i).c_str());}

最后输出为:

###192###

###168###

###0###

###1###

0 0