Advertisement
lalkaed

Egz

Jun 29th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.17 KB | None | 0 0
  1. #include <iostream>
  2.  
  3.  
  4.  
  5.  
  6. template <typename T>
  7. class Vector{
  8.     T* ptr;
  9.     int size;
  10.     int curr;
  11. public:
  12.     Vector(){
  13.         this->size = 2;
  14.         this->ptr = new T[this->size];
  15.         this->curr = 0;
  16.     }
  17.     ~Vector(){
  18.         delete [] this->ptr;
  19.     }
  20.     void add(T &t){
  21.         ++this->curr;
  22.         if (this->curr>this->size){
  23.             this->size=this->size+100;
  24.             T* tmp = new T[this->size];
  25.             for (int i = 0; i < this->curr; ++i) {
  26.                 tmp[i]=this->ptr[i];
  27.             }
  28.             delete this->ptr;
  29.             this->ptr=tmp;
  30.         }
  31.         this->ptr[curr]=t;
  32.     }
  33.  
  34.     Vector(const Vector &prev) {
  35.  
  36.         this->size=prev.size;
  37.         this->curr=prev.curr;
  38.         this->ptr = new T[this->size];
  39.         for (int i = 0; i < this->curr; ++i) {
  40.             this->ptr[i]=prev.ptr[i];
  41.             std::cout << this->ptr[i]<<", ";
  42.         }
  43.         std::cout << std::endl<<"copy ended"<<std::endl;
  44.     }
  45.  
  46. };
  47.  
  48.  
  49.  
  50. int main() {
  51.     Vector <int>vec;
  52.     Vector <int>ve(vec);
  53.     int n = 2;
  54.     vec.add(n);
  55.     vec.add(n);
  56.     vec.add(n);
  57.     vec.add(n);
  58.     vec.add(n);
  59.     return 0;
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement