#include #include #include #include #include typedef boost::algorithm::split_iterator splitter; splitter make_splitter(const std::string& s) { return boost::algorithm::make_split_iterator(s, boost::first_finder("/")); } void print(const splitter& it, std::string prefix = std::string()) { std::cout << prefix << "val='" << boost::copy_range(*it) << "', eof=" << it.eof() << std::endl; } void parse(splitter it) { if (it == splitter()) return; print(it, "before: "); if (it->empty()) { std::cout << "Note that eof was reset to false, not copied!" << std::endl; exit(1); // Avoid endless loop; we've proved our point. } ++it; print(it, "after: "); std::cout << std::endl; parse(it); // Pass 'it' by value, calling copy ctor. } int main() { std::string s = "abc/def/ghi/jkl"; std::cout << "iterative version (shared iter):" << std::endl; for (splitter it = make_splitter(s); it != splitter(); ++it) print(it); std::cout << std::endl; std::cout << "recursive version (copied iter):" << std::endl; parse(make_splitter(s)); }