码迷,mamicode.com
首页 > 其他好文 > 详细

字符串分隔

时间:2016-06-21 22:15:21      阅读:138      评论:0      收藏:0      [点我收藏+]

标签:

I use this to split string by a delim.

The first puts the results in a pre-constructed vector,

the second returns a new vector.

#include <string>
#include <sstream>
#include <vector>

using namespace std;

vector<string> &split(const string &s, char delim, vector<string> &elems) {
    stringstream ss(s);
    string item;
    while (getline(ss, item, delim)) {
        elems.push_back(item);
    }
    return elems;
}


vector<string> split(const string &s, char delim) {
    vector<string> elems;
    split(s, delim, elems);
    return elems;
}

Note that this solution does not skip empty tokens, so the following will find 4 items, one of which is empty:

vector<string> x = split("one:two::three", :);

参见:

       from

字符串分隔

标签:

原文地址:http://www.cnblogs.com/Searchor/p/5604975.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!