LazyString拆分器:
#include <string>
#include <algorithm>
#include <unordered_set>
using namespace std;
class LazyStringSplitter
{
string::const_iterator start, finish;
unordered_set<char> chop;
public:
// Empty Constructor
explicit LazyStringSplitter()
{}
explicit LazyStringSplitter (const string cstr, const string delims)
: start(cstr.begin())
, finish(cstr.end())
, chop(delims.begin(), delims.end())
{}
void operator () (const string cstr, const string delims)
{
chop.insert(delims.begin(), delims.end());
start = cstr.begin();
finish = cstr.end();
}
bool empty() const { return (start >= finish); }
string next()
{
// return empty string
// if ran out of characters
if (empty())
return string("");
auto runner = find_if(start, finish, [&](char c) {
return chop.count(c) == 1;
});
// construct next string
string ret(start, runner);
start = runner + 1;
// Never return empty string
// + tail recursion makes this method efficient
return !ret.empty() ? ret : next();
}
};
我将此方法称为LazyStringSplitter是因为一个原因——它不会一次性拆分字符串。本质上,它的行为类似于python生成器它公开了一个名为next的方法,该方法返回从原始字符串拆分的下一个字符串我使用了c++11STL中的无序集,因此查找分隔符的速度要快得多下面是它的工作原理
测试程序
#include <iostream>
using namespace std;
int main()
{
LazyStringSplitter splitter;
// split at the characters ' ', '!', '.', ','
splitter("This, is a string. And here is another string! Let's test and see how well this does.", " !.,");
while (!splitter.empty())
cout << splitter.next() << endl;
return 0;
}
输出,输出
This
is
a
string
And
here
is
another
string
Let's
test
and
see
how
well
this
does
改进这一点的下一个计划是实施开始和结束方法,以便可以执行以下操作:
vector<string> split_string(splitter.begin(), splitter.end());