C++ STL 切分字符串

来源:互联网 发布:pdf.js ajax 编辑:程序博客网 时间:2024/06/07 06:34

pattern是分隔符,如”.”。
返回被切分的一系列子串。

std::vector< std::string > splitString_STL(const std::string& str, const std::string& pattern){    std::vector<std::string> subStrings;    if (str.empty())        return subStrings;    std::string strs = str + pattern;    size_t pos = strs.find(pattern);    size_t size = strs.size();    while (pos != std::string::npos) {        std::string sub = strs.substr(0,pos);        subStrings.push_back(sub);        strs = strs.substr(pos+1,size);        pos = strs.find(pattern);    }    return subStrings;}
原创粉丝点击