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
|
#include <iostream>
#define vector Vector
using namespace std;
template <class T> class My_allocator {
public:
T* allocate(int n); // allocate space for n objects of type T
void deallocate(T* p, int n); // deallocate n objects of type T starting at p
void construct(T* p, const T& v); // construct a T with the value v in p
void destroy(T* p); // destroy the T in p
};
//-------------------------------------------------
template<class T>
T* My_allocator<T>::allocate(int n)
{
T* p = (T*)malloc(sizeof(T)*n);
return p;
}
//------------------------------
template<class T>
void My_allocator<T>::deallocate(T* p, int n)
{
free(p);
}
//------------------------------
template<class T>
void My_allocator<T>::construct(T* p, const T& v)
{
p = new (p) T(v);
}
//------------------------------
template<class T>
void My_allocator<T>::destroy(T* p)
{
p->~T();
}
//------------------------------------------------
class Range_error : out_of_range {
public:
int index;
Range_error(int i) :out_of_range("Range error"), index(i) {}
};
//--------------------------------------------------
template <class T, class A = My_allocator<T> >
class vector{
A alloc;
int sz, space;
T* elem;
public:
vector() :sz(0), space(0), elem(nullptr) {}
vector(int n) :sz(n), space(n) { elem = alloc.allocate(n); initial(T()); }
vector(int n, const T& v) :sz(n), space(n) { elem = alloc.allocate(n); initial(v); }
int size() const { return sz; }
int capacity() const { return space; }
T get_elem(int i) { return elem[i]; }
void initial(const T);
void resize(int, T);
void push_back(const T&);
void reserve(int);
T& operator[](unsigned int n) // rather than return at(i);
{
if (n < 0 || this->sz <= n) throw Range_error(n);
return elem[n];
}
const T& operator[](unsigned int n) const
{
if (n < 0 || this->sz <= n) throw Range_error(n);
return elem[n];
}
};
//------------------------------------------------
template<class T, class A>
void vector<T, A>::initial(const T v)
{
for (int i = 0; i < sz; ++i)
elem[i] = v;
}
//-------------------------------------------
template <class T, class A>
void vector<T, A>::resize(int newsize, T val = T())
{
reserve(newsize);
for (int i = sz; i < newsize; ++i) alloc.construct(&elem[i], val); // construct
for (int i = newsize; i < sz; ++i) alloc.destroy(&elem[i]); // destroy
sz = newsize;
}
//--------------------------------------------
template <class T, class A>
void vector<T, A>::push_back(const T& val)
{
if (space == 0) reserve(8);
else if (sz == space) reserve(2 * space);
alloc.construct(&elem[sz], val);
++sz;
}
//--------------------------------------
template <class T, class A>
void vector<T, A>::reserve(int newalloc)
{
if (newalloc <= space) return;
T* p = alloc.allocate(newalloc);
for (int i = 0; i < sz; ++i) alloc.construct(&p[i], elem[i]);
for (int i = 0; i < sz; ++i) alloc.destroy(&elem[i]);
alloc.deallocate(elem, space);
elem = p;
space = newalloc;
}
//----------------------------------
int main() {
vector<int> v(4,3);
v.push_back(12);
for (int i = 0; i < v.size(); ++i)
cout << "'" << v[i] << "'" << " ";
cout << "\n\n";
v.reserve(10);
for (int i = 0; i < v.size(); ++i)
cout << "'" << v[i] << "'" << " ";
cout << "\n\n";
v.resize(12, 5);
for (int i = 0; i < v.size(); ++i)
cout << "'" << v[i] << "'" << " ";
return 0;
}
|