没有Boost,没有字符串流,只有标准的C库与std::string和std::list:C库函数配合使用,便于分析,C++数据类型便于内存管理。
空白被认为是换行符、制表符和空格的任意组合。空白字符集由wschars变量建立。
#include <string>
#include <list>
#include <iostream>
#include <cstring>
using namespace std;
const char *wschars = "\t\n ";
list<string> split(const string &str)
{
const char *cstr = str.c_str();
list<string> out;
while (*cstr) { // while remaining string not empty
size_t toklen;
cstr += strspn(cstr, wschars); // skip leading whitespace
toklen = strcspn(cstr, wschars); // figure out token length
if (toklen) // if we have a token, add to list
out.push_back(string(cstr, toklen));
cstr += toklen; // skip over token
}
// ran out of string; return list
return out;
}
int main(int argc, char **argv)
{
list<string> li = split(argv[1]);
for (list<string>::iterator i = li.begin(); i != li.end(); i++)
cout << "{" << *i << "}" << endl;
return 0;
}
Run:
$ ./split ""
$ ./split "a"
{a}
$ ./split " a "
{a}
$ ./split " a b"
{a}
{b}
$ ./split " a b c"
{a}
{b}
{c}
$ ./split " a b c d "
{a}
{b}
{c}
{d}
split的尾部递归版本(本身分裂为两个函数)。除了将字符串推入列表之外,所有对变量的破坏性操作都消失了!
void split_rec(const char *cstr, list<string> &li)
{
if (*cstr) {
const size_t leadsp = strspn(cstr, wschars);
const size_t toklen = strcspn(cstr + leadsp, wschars);
if (toklen)
li.push_back(string(cstr + leadsp, toklen));
split_rec(cstr + leadsp + toklen, li);
}
}
list<string> split(const string &str)
{
list<string> out;
split_rec(str.c_str(), out);
return out;
}