首页 > 其他 > 详细

字符串分隔

时间:2016-06-21 22:13:10      阅读:119      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!