Split函数封装

vector<string> split(string& str, char ch)//字符串分割函数
{
    vector<string> res;
    //加入一个分割字符,方便操作
    str += ch;
    //分割的起始位置
    int start = 0;
    //分割的终止位置
    int last = str.find(ch);
    while (last < str.size())
    {
        //如果字符串不为空,则添加到结果中
        if (start != last)
            res.push_back(str.substr(start, last - start));
        //起始位置更新
        start = last + 1;
        //终止位置更新
        last = str.find(ch, start);
    }
    return res;
}

2 Replies to “Split函数封装”

  1. Nice post. I was checking continuously this
    blog and I’m impressed! Very useful information particularly the
    last part 🙂 I care for such information much.
    I was seeking this certain info for a long time. Thank you and best of luck.

发表评论

电子邮件地址不会被公开。 必填项已用*标注