-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReverse_iterator.h
88 lines (75 loc) · 1.42 KB
/
Reverse_iterator.h
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#pragma once
#ifndef _REVERSE_ITERATOR
#define _REVERSE_ITERATOR
#include"iterator.h"
namespace ministl
{
template<class iterator>
class Reverse_iterator
{
public:
typedef iterator iterator_type;
typedef typename iterator_traits<iterator>::value_type value_type;
typedef typename iterator_traits<iterator>::reference reference;
typedef typename iterator_traits<iterator>::pointer pointer;
typedef typename iterator_traits<iterator>::difference_type difference_type;
typedef Reverse_iterator<iterator> self;
private:
iterator pos;
public:
Reverse_iterator() :pos() {}
Reverse_iterator(iterator iter) :pos(iter) {}
Reverse_iterator(const self& rhs) :pos(rhs.pos) {}
bool operator==(const self& rhs)
{
return pos == rhs.pos;
}
bool operator!=(const self& rhs)
{
return !(*this == rhs);
}
self& operator++()
{
pos--;
return *this;
}
self operator++(int)
{
auto tmp = *this;
pos--;
return tmp;
}
self& operator--()
{
pos++;
return *this;
}
self operator--(int)
{
auto tmp = *this;
pos++;
return tmp;
}
self& operator=(const self& rhs)
{
pos = rhs.pos;
return *this;
}
self& operator+(difference_type n)
{
ministl::advance(pos, -n);
return *this;
}
self& operator-(difference_type n)
{
ministl::advance(pos, n);
return *this;
}
value_type operator*()
{
iterator tmp = pos;
return *(--tmp);
}
};
}
#endif