简单的string split

static void split_string(const std::string& src_str, const char* delim, std::vector<std::string>& tokens) {
    size_t delim_len = strlen(delim);
    std::string::size_type pos = 0;
    while(pos < src_str.size()) {
        std::string::size_type next_pos = src_str.find(delim, pos);
        if(std::string::npos == next_pos)
            next_pos = src_str.size();

        if(pos < next_pos)
            tokens.emplace_back(src_str.substr(pos, next_pos - pos));

        if(pos == next_pos)
            tokens.emplace_back("");

        pos = next_pos + delim_len;
    }
}

 



posted @ 2020-01-02 20:10  water_bear  阅读(252)  评论(0编辑  收藏  举报