Loading

C++实现分割字符串函数

由于C++没有现成的字符串分割函数,所以自己封装一个函数

vector<string> split(const string& s, const string& sep) {
    vector<string> v;
    auto pos2 = s.find(sep);
    auto pos1 = 0;
    while (s.npos != pos2) {
        v.push_back(s.substr(pos1, pos2 - pos1));
        pos1 = pos2 + sep.size();
        pos2 = s.find(sep, pos1);
    }
    if (pos1 != s.length()) v.push_back(s.substr(pos1));
    return v;
}

 

posted @ 2020-05-05 21:46  金砖丶  阅读(345)  评论(0)    收藏  举报