-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathboost_iterator_facade.cpp
More file actions
52 lines (37 loc) · 1.16 KB
/
boost_iterator_facade.cpp
File metadata and controls
52 lines (37 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
#include <boost/iterator/iterator_facade.hpp>
// Single linked list node
struct Node {
string value;
Node *next = nullptr;
explicit Node(const string &value) : value(value) {}
Node(const string &value, Node *const parent) : value(value) {
parent->next = this;
}
};
// Linked List iterator based on boost::iterator_facade.
// Requires implementation of: increment(), equal(), and dereference() methods.
struct ListIterator
: boost::iterator_facade<ListIterator, Node, boost::forward_traversal_tag> {
Node *current = nullptr;
ListIterator() {}
explicit ListIterator(Node *const current) : current(current) {}
private:
friend class boost::iterator_core_access;
void increment() { current = current->next; }
bool equal(const ListIterator &other) const {
return other.current == current;
};
Node &dereference() const { return *current; }
};
int main() {
Node alpha{"alpha"};
Node beta{"beta", &alpha};
Node gamma{"gamma", &beta};
for_each(ListIterator{&alpha}, ListIterator{},
[](const Node &n) { cout << n.value << endl; });
return 0;
}