C++分割string字符串(转)

原文链接:https://blog.csdn.net/jirryzhang/article/details/80473032

或:https://www.cnblogs.com/dingxiaoqiang/p/8228390.html

vector<string> split(const string& str, const string& pattern)
{
    vector<string> ret;
    if(pattern.empty()) return ret;
    size_t start=0,index=str.find_first_of(pattern,0);
    while(index!=str.npos)
    {
        if(start!=index)
            ret.push_back(str.substr(start,index-start));
        start=index+1;
        index=str.find_first_of(pattern,start);
    }
    if(!str.substr(start).empty())
        ret.push_back(str.substr(start));
    return ret;
}
posted @ 2019-11-17 09:11  姬无华  阅读(1694)  评论(0编辑  收藏  举报