-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path12-12.cpp
More file actions
165 lines (129 loc) · 3.83 KB
/
12-12.cpp
File metadata and controls
165 lines (129 loc) · 3.83 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
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include <iostream>
#include <memory>
#include <algorithm>
#include <string>
#include <vector>
using std::allocator;
using std::cout;
using std::endl;
using std::max;
using std::uninitialized_copy;
using std::uninitialized_fill;
using std::ptrdiff_t;
using std::string;
using std::vector;
template <class T> class Vec {
public:
typedef T* iterator;
typedef const T* const_iterator;
typedef size_t size_type;
typedef T value_type;
typedef T& reference;
typedef const T& const_reference;
Vec() { create(); } // default constructor
explicit Vec(size_type n, const T& t = T()) { create(n, t); }
Vec(const Vec& v) { create(v.begin(), v.end()); } // copy constructor
Vec& operator=(const Vec&);
~Vec() {uncreate(); }
T& operator[](size_type i) { return data[i]; }
const T& operator[](size_type i) const { return data[i]; }
void push_back(const T& t) {
if (avail == limit)
grow();
unchecked_append(t);
}
void insert(iterator pos, iterator first, iterator last) {
size_type delta = last - first;
// grow Vec size enough to incorporate the additional range
while ((limit - avail) < delta)
grow();
// initialize current as the object at the back of Vec
iterator currentO = avail-1;
iterator currentN = last-1;
// invariant: currentO is the back-most object in Vec that has not been
// ovewritten with the new range.
while (currentO >= pos) {
alloc.construct(currentO+delta, *currentO);
*currentO = *currentN;
if (currentO == begin() || currentN == first)
break;
else {
--currentO;
--currentN;
}
}
avail = avail + delta;
}
size_type size() const { return avail - data; }
iterator begin() { return data; }
const_iterator begin() const { return data; }
iterator end() { return avail; }
const_iterator end() const { return avail; }
void uncreate();
private:
iterator data;
iterator avail;
iterator limit;
allocator<T> alloc;
void create();
void create(size_type, const T&);
void create(const_iterator, const_iterator);
void grow();
void unchecked_append(const T&);
};
template <class T> void Vec<T>::create()
{
data = avail = limit = 0;
}
template <class T> void Vec<T>::create(size_type n, const T& val)
{
data = alloc.allocate(n);
limit = avail = data + n;
uninitialized_fill(data, limit, val);
}
template <class T>
void Vec<T>::create(const_iterator i, const_iterator j)
{
data = alloc.allocate(j - i);
limit = avail = uninitialized_copy(i, j, data);
}
template <class T> void Vec<T>::uncreate()
{
if (data) {
iterator it = avail;
while (it != data)
alloc.destroy(--it);
alloc.deallocate(data, limit - data);
}
data = limit = avail = 0;
}
template <class T> void Vec<T>::grow()
{
size_type new_size = max(2 * (limit - data), ptrdiff_t(1));
iterator new_data = alloc.allocate(new_size);
iterator new_avail = uninitialized_copy(data, avail, new_data);
uncreate();
data = new_data;
avail = new_avail;
limit = data + new_size;
}
template <class T> void Vec<T>::unchecked_append(const T& val)
{
alloc.construct(avail++, val);
}
int main()
{
Vec<string> mtnbiking;
mtnbiking.push_back("berms");
mtnbiking.push_back("fireroads");
mtnbiking.push_back("singletrack");
Vec<string> temp;
temp.push_back("hardtail");
temp.push_back("softtail");
Vec<string>::iterator insertPos = mtnbiking.begin()+2;
mtnbiking.insert(insertPos, temp.begin(), temp.end());
for (Vec<string>::iterator it = mtnbiking.begin(); it != mtnbiking.end(); it++) {
cout << *it << endl;
}
return 0;
}