1 | #include <iostream>
|
---|
2 | #include <string>
|
---|
3 | #include <boost/algorithm/string/find_iterator.hpp>
|
---|
4 | #include <boost/algorithm/string/finder.hpp>
|
---|
5 | #include <boost/range/iterator_range.hpp>
|
---|
6 |
|
---|
7 | typedef boost::algorithm::split_iterator<std::string::const_iterator> splitter;
|
---|
8 |
|
---|
9 | splitter make_splitter(const std::string& s)
|
---|
10 | {
|
---|
11 | return boost::algorithm::make_split_iterator(s, boost::first_finder("/"));
|
---|
12 | }
|
---|
13 |
|
---|
14 | void print(const splitter& it, std::string prefix = std::string())
|
---|
15 | {
|
---|
16 | std::cout << prefix << "val='" << boost::copy_range<std::string>(*it) << "', eof=" << it.eof() << std::endl;
|
---|
17 | }
|
---|
18 |
|
---|
19 | void parse(splitter it)
|
---|
20 | {
|
---|
21 | if (it == splitter())
|
---|
22 | return;
|
---|
23 |
|
---|
24 | print(it, "before: ");
|
---|
25 | if (it->empty())
|
---|
26 | {
|
---|
27 | std::cout << "Note that eof was reset to false, not copied!" << std::endl;
|
---|
28 | exit(1); // Avoid endless loop; we've proved our point.
|
---|
29 | }
|
---|
30 |
|
---|
31 | ++it;
|
---|
32 | print(it, "after: ");
|
---|
33 | std::cout << std::endl;
|
---|
34 |
|
---|
35 | parse(it); // Pass 'it' by value, calling copy ctor.
|
---|
36 | }
|
---|
37 |
|
---|
38 | int main()
|
---|
39 | {
|
---|
40 | std::string s = "abc/def/ghi/jkl";
|
---|
41 |
|
---|
42 | std::cout << "iterative version (shared iter):" << std::endl;
|
---|
43 | for (splitter it = make_splitter(s); it != splitter(); ++it)
|
---|
44 | print(it);
|
---|
45 | std::cout << std::endl;
|
---|
46 |
|
---|
47 | std::cout << "recursive version (copied iter):" << std::endl;
|
---|
48 | parse(make_splitter(s));
|
---|
49 | }
|
---|