Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Lazy String Tokenizer Class
#include <string> #include <algorithm> #include <unordered_set> #include <iostream> #include <cstdlib> using namespace std; class LazyStringSplitter { string::const_iterator start, finish; unordered_set<char> chop; explicit LazyStringSplitter() {} public: 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(); } }; int main() { LazyStringSplitter splitter("This, is a string. And here is another string! Let's test and see how well this does.", " !.,"); // 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 EXIT_SUCCESS; }
run
|
edit
|
history
|
help
0
Teatime Snack
logcOperator
Metodos mejorado
Day3
sorting
Test
constructing object on first use as return value of (pointer to) object-returning function
Updated Linked Lists - 5/10/2017 V4.0
MeanSDVar
CutRod(BottomUp)