1 | #include <boost/iterator/iterator_facade.hpp>
|
---|
2 |
|
---|
3 | #include <vector>
|
---|
4 | #include <iterator>
|
---|
5 | #include <iostream>
|
---|
6 |
|
---|
7 | class vector_iterator
|
---|
8 | : public boost::iterator_facade<
|
---|
9 | vector_iterator,
|
---|
10 | int,
|
---|
11 | boost::random_access_traversal_tag>
|
---|
12 | {
|
---|
13 | public:
|
---|
14 |
|
---|
15 | vector_iterator(char tag, std::vector<int>::iterator it) : _tag(tag), _it(it) {}
|
---|
16 |
|
---|
17 | private:
|
---|
18 | friend class boost::iterator_core_access;
|
---|
19 |
|
---|
20 | int& dereference() const { return *_it; }
|
---|
21 | bool equal(vector_iterator const& other) const { return _it == other._it; }
|
---|
22 | void increment() { ++_it; }
|
---|
23 | void decrement() { --_it; }
|
---|
24 | void advance(int n) { std::advance(_it, n); }
|
---|
25 |
|
---|
26 | std::vector<int>::iterator::difference_type
|
---|
27 | distance_to(const vector_iterator& other) const
|
---|
28 | {
|
---|
29 | std::cerr << _tag << ".distance_to(" << other._tag << ")\n";
|
---|
30 | std::vector<int>::iterator::difference_type result = std::distance(_it, other._it);
|
---|
31 | std::cerr << "result = " << result << '\n';
|
---|
32 | return result;
|
---|
33 | }
|
---|
34 |
|
---|
35 | private:
|
---|
36 | const char _tag;
|
---|
37 | std::vector<int>::iterator _it;
|
---|
38 | };
|
---|
39 |
|
---|
40 | int main()
|
---|
41 | {
|
---|
42 | std::vector<int> v;
|
---|
43 | v.push_back(1);
|
---|
44 | v.push_back(2);
|
---|
45 |
|
---|
46 | vector_iterator a('a', v.begin());
|
---|
47 | vector_iterator b('b', v.end());
|
---|
48 |
|
---|
49 | std::cerr << std::distance(a, b) << '\n';
|
---|
50 | }
|
---|