C++ 版本的split_string

vector<string> split_string(const string &in, char del, bool skip_empty) {
    vector<string> res;

    if (in.empty() || del == '\0')
        return res;

    string field;
    istringstream f(in);
    if (del == '\n') {
        while(getline(f, field)) {
            if (field.empty() && skip_empty)
                continue;
            res.push_back(field);
        }
    } else {
        while(getline(f, field, del)) {
            if (field.empty() && skip_empty)
                continue;
            res.push_back(field);
        }
    }
    return res;
}

 

posted @ 2014-04-12 17:54  sndnvaps  阅读(714)  评论(0编辑  收藏  举报