-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVector4.c++
88 lines (72 loc) · 1.98 KB
/
Vector4.c++
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
template <typename T, typename A = allocator<T>> class my_vector {
friend bool operator==(const my_vector &lhs, const my_vector &rhs) {
return (lhs.size() == rhs.size()) &&
equal(lhs.begin(), lhs.end(), rhs.begin());
}
friend bool operator<(const my_vector &lhs, const my_vector &rhs) {
return lexicographical_compare(lhs.begin(), lhs.end(), rhs.begin(),
rhs.end());
}
public:
using pointer = T *;
using iterator = pointer;
private:
A a;
pointer _b = nullptr;
pointer _e = nullptr;
public:
my_vector() = default;
explicit my_vector(int s) {
if (s != 0)
_b = a.allocate(s);
_e = _b + s;
my_uninitialized_fill(a, _b, _e, T());
}
my_vector(int s, const T &v) {
if (s != 0)
_b = a.allocate(s);
_e = _b + s;
my_uninitialized_fill(a, _b, _e, v);
}
my_vector(int s, const T &v, allocator<T> t) : a(t) {
if (s != 0)
_b = a.allocate(s);
_e = _b + s;
my_uninitialized_fill(a, _b, _e, v);
}
my_vector(initializer_list<T> rhs) {
if (rhs.size() != 0)
_b = a.allocate(rhs.size());
_e = _b + rhs.size();
my_uninitialized_copy(a, rhs.begin(), rhs.end(), _b);
}
my_vector(const my_vector &rhs) {
if (rhs.size() != 0)
_b = a.allocate(rhs.size());
_e = _b + rhs.size();
my_uninitialized_copy(a, rhs.begin(), rhs.end(), _b);
}
my_vector(my_vector &&rhs) {
std::swap(_b, rhs._b);
std::swap(_e, rhs._e);
}
my_vector &operator=(const my_vector &rhs) {
my_vector t(rhs);
std::swap(_b, t._b);
std::swap(_e, t._e);
return *this;
}
my_vector &operator=(my_vector &&rhs) {
my_vector t(move(rhs));
std::swap(_b, t._b);
std::swap(_e, t._e);
return *this;
}
~my_vector() { my_destroy(a, _b, _e); }
T &operator[](int i) const {
return _b[i];
} // warning: Returning null reference
iterator begin() const { return _b; }
iterator end() const { return _e; }
int size() const { return _e - _b; }
};