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 166 167 168 169 170 171 172 173 174 175 176 177 178
|
#include <iostream>
#define pvector 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 pvector {
A alloc;
int sz, space;
T elem;
public:
pvector() :sz(0), space(0), elem(nullptr) {}
pvector(int n) :sz(n), space(n) { elem = alloc.allocate(n); initial(); }
pvector(int n, const T& v) :sz(n), space(n) { elem = alloc.allocate(n); initial(v); }
pvector<T>& operator=(const pvector<T>); // Copy assignment
// Copy constructor
int size() const { return sz; }
int capacity() const { return space; }
void initial();
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];
}
~pvector() { delete elem; } // Destructor
};
//------------------------------------------------
template<class T, class A>
pvector<T>& pvector<T, A>::operator=(const pvector<T> a)
{
if (a.size() <= space) {
for (int i = 0; i < a.size(); ++i)
elem[i] = a.elem[i];
sz = a.size();
return *this;
}
else if (a.size() > space) {
T* p = alloc.allocate(a.size());
for (int i = 0; i < sz; ++i) alloc.construct(&p[i], a.elem[i]);
for (int i = 0; i < sz; ++i) alloc.destroy(&elem[i]);
alloc.deallocate(elem, space);
elem = p;
space = sz = a.size();
return *this;
}
}
//---------------------------------------------
template<class T, class A>
void pvector<T, A>::initial()
{
for (int i = 0; i < sz; ++i)
elem[i] = 0;
}
//-------------------------------------------
template <class T, class A>
void pvector<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 pvector<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 pvector<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() {
int i = 5;
int* pi = &i;
pvector<int*> v1;
v1.push_back(pi);
*v1[0] = 10;
cout << v1.size() << ' ' << v1.capacity() << ' '
<< *v1[0] << ' ' << v1[0] << ' ' << endl;
pvector<int*> v2(3);
cout << v2.size() << ' ' << v2.capacity() << ' '
<< *v2[0] << ' ' << v2[0] << ' ' << endl;
system("pause");
return 0;
}
|