Hello,

Here is the exercise no.15. Well, just like many other exercises it's not comprehensible well enough. I wrote this for that:

It shows "Code:#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(T()); } pvector(int n, const T& v) :sz(n), space(n) { elem = alloc.allocate(n); initial(v); } pvector<T>& operator=(const pvector<T>); //Assignment Operator 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 pvector<T, A>::initial(const T v) { for (int i = 0; i < sz; ++i) elem[i] = v; } //-------------------------------------------- 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; } //---------------------------------- int main() { int i = 5; int* pi = &i; pvector<int*> v; v.push_back(pi); cout << v.size() << ' ' << v.capacity() << ' ' << *v.get_elem(0) << ' ' << v.get_elem(0) << endl; system("pause"); return 0; }" in output.1 8 5 an address

There are some member functions that aren't through yet.

My question is, can it be a good code for that exercise (of course after doing the member functions' implementations)?