-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.cpp
57 lines (41 loc) · 1.62 KB
/
example.cpp
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
53
54
55
56
57
// Use of this source code is governed by a BSD-style license that can
// be found in the LICENSE file at the root of the source tree or at
// <https://github.com/Krzmbrzl/iterators/blob/main/LICENSE>.
#include <iterators/iterator_facade.hpp>
#include <cstddef>
#include <iostream>
#include <iterator>
struct MyCore {
// Declare what iterator category we are aiming for
using target_iterator_category = std::input_iterator_tag;
MyCore(int *ptr) : m_ptr(ptr) {}
MyCore(const MyCore &) = default;
MyCore(MyCore &&) = default;
~MyCore() = default;
auto operator=(const MyCore &) -> MyCore & = default;
auto operator=(MyCore &&) -> MyCore & = default;
// Required from forward iterators onwards
MyCore() = default;
[[nodiscard]] auto dereference() const -> int & { return *m_ptr; }
void increment() { m_ptr += 1; }
// Required for all iterator categories except output iterators
[[nodiscard]] auto equals(const MyCore &other) const -> bool { return m_ptr == other.m_ptr; }
// Required only for bidirectional and random access iterators
void decrement() { m_ptr -= 1; }
// Required only for random access iterators
[[nodiscard]] auto distance_to(const MyCore &other) const -> std::ptrdiff_t { return other.m_ptr - m_ptr; }
// Required only for random access iterators
void advance(std::ptrdiff_t amount) { m_ptr += amount; }
private:
int *m_ptr = nullptr;
};
using MyIterator = iterators::iterator_facade< MyCore >;
auto main() -> int {
int numbers[3] = { 1, 2, 3 };
MyIterator iter(MyCore{ numbers });
std::cout << *iter << "\n";
iter++;
std::cout << *iter << "\n";
++iter;
std::cout << *iter << "\n";
}