c++中分割字符串

vector<string> split(string str, string pattern)

//自定义字符串分割函数
//字符串分割函数
vector<string> split(string str, string pattern)
{
    string::size_type pos;
    vector<string> result;
    str += pattern;//扩展字符串以方便操作
    int size = str.size();

    for (int i = 0; i < size; i++)
    {
        pos = str.find(pattern, i);
        if (pos < size)
        {
            string s = str.substr(i, pos - i);
            //int int_s = atoi(s.c_str());
            result.push_back(s);
            i = pos + pattern.size() - 1;
        }
    }
    return result;
}

atoi(s.c_str())

把string类型的一串数字转为int:

int int_s = atoi(s.c_str());

 

posted @ 2021-10-27 21:33  PiaYie  阅读(68)  评论(0编辑  收藏  举报