[C/C++] 简单实现按字符分割字符串split函数

记录一下

/**
 * 字符串 str 通过字符 target 进行分割
 */
vector<string> split(string str, char target) {
    vector<string> res;
    int pos = 0;
    while (pos < str.size()) {
        while (pos < str.size() && str[pos] == target) {    // 移动到片段开头
            pos++;
        }
        int start = pos;                                    // 记录片段开头
        while (pos < str.size() && str[pos] != target) {    // 移动到片段末尾的下一个下标
            pos++;
        }
        res.push_back(str.substr(start, pos - start));      // 切割字符串,并切割的片段添加到数组中
    }
    return res;
}

 

原文链接: https://www.cnblogs.com/chasemeng/p/17063905.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    [C/C++] 简单实现按字符分割字符串split函数

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/313441

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月16日 下午12:50
下一篇 2023年2月16日 下午12:51

相关推荐