c++ 字符串分割

void split(const string& str, vector<string>& ret_, string sep)--传入要分割的字符串str,要存储分割字符串的ret,分割的字符串str

{

    if (str.empty())

    {

        return;

    }

    

    string tmp;

    string::size_type pos_begin = str.find_first_not_of(sep);

    string::size_type comma_pos = 0;

    

    while (pos_begin != string::npos)

    {

        comma_pos = str.find(sep, pos_begin);

        if (comma_pos != string::npos)

        {

            tmp = str.substr(pos_begin, comma_pos - pos_begin);

            pos_begin = comma_pos + sep.length();

        }

        else

        {

            tmp = str.substr(pos_begin);

            pos_begin = comma_pos;

        }

        

        if (!tmp.empty())

        {

            ret_.push_back(tmp);

            tmp.clear();

        }

    }

}

原文地址:https://www.cnblogs.com/HemJohn/p/5205624.html