Реализуйте vector, применяя аллокаторы, а не операции new и delete.
Простая задачка. Я не все функции переопределил, просто не охота морочить голову с функциями, это так для принципа, главное понять принцип, а потом можно все определить, если понадобиться.
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 |
#include <iostream> using std::cout; using std::endl; #include <allocators> using std::allocator; template<typename T,typename A=allocator<T> > class vector { public: typedef T value_type; typedef T* iterator; typedef T& reference; typedef allocator<T> allocator_type; typedef unsigned int size_type; vector():mass(0),s(0){} vector(size_type n):s(n){mass=alloc.allocate(n);} size_type size(){return s;} value_type& operator[](int i){return mass[i];} iterator begin(){return &mass[0];} iterator end(){return &mass[s];} value_type& operator*(){return *mass;} iterator operator++(){return &++mass;} private: size_type s; T* mass; A alloc; }; int main() { cout <<"realizaci9 vektor c allocatorom"<<endl; vector<int> v(5); cout <<"v.size()= "<<v.size()<<endl; for(int i=0;i<5;i++) { v[i]=i; cout <<v[i]<<' '; } cout <<endl; vector<int>::iterator it; it=v.begin(); cout <<"*it= "<<*it<<endl; for(it=v.begin();it!=v.end();++it) cout <<*it<<' '; cout <<endl; return 0; } |
[youtube]http://www.youtube.com/watch?v=mgeOFqKe5MI[/youtube]