Advertisement
Timtsa

lesson 2

Nov 30th, 2018
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.89 KB | None | 0 0
  1. # include <iostream>
  2. # include <algorithm>
  3. using namespace std;
  4.  
  5.  
  6.  
  7.  
  8. class MyVector
  9. {
  10. private:
  11.     size_t  size;
  12.     int * data;
  13.     size_t capacity;
  14. public:
  15.     MyVector()
  16.     {
  17.         size = capacity = 0;
  18.         data = nullptr;  
  19.         cout << "c'tor 1" << endl;
  20.     }
  21.     MyVector(size_t _size,int velue=0)
  22.     {
  23.         size = _size;
  24.         capacity = size + size / 2;
  25.         data = new  int[capacity];
  26.         fill(data, data + capacity, velue);
  27.         //cout << "c'tor 2" << endl;
  28.     }
  29.  
  30.     bool operator == (const MyVector &other) const //Константный метод
  31.     {
  32.         if (size != other.size) return false;
  33.         for (size_t i = 0; i < size; i++)
  34.         {
  35.             if ( data[i]!=other.data[i])
  36.             {
  37.                 return false;
  38.             }
  39.            
  40.         }
  41.         return true;
  42.     }
  43.  
  44.     MyVector& operator = (const MyVector & other)
  45.     {
  46.         if (this == &other) return *this;
  47.         delete[] data;
  48.         size = other.size;
  49.         capacity = other.capacity;
  50.         data = new int[capacity];
  51.         copy(other.data, other.data + capacity, data);
  52.         return * this;
  53.  
  54.     }
  55.  
  56.  
  57.     bool operator != (const MyVector & other) const
  58.     {
  59.         return !(*this == other);
  60.     }
  61.    
  62.     //MyVector()
  63.     //{
  64.     //}
  65.    
  66.    
  67.     void print()const
  68.     {
  69.         for (size_t i = 0; i < size; i++)
  70.         {
  71.             cout << data[i] << ' ';
  72.         }
  73.    
  74.         cout << endl;
  75.     }
  76.     MyVector(const MyVector &other)
  77.     {
  78.         this->size = other.size;
  79.         this->capacity = other.capacity;
  80.         this->data = new int[this->capacity];
  81.         copy(other.data, other.data + capacity, this->data);
  82.     }
  83.    
  84.    
  85.    
  86.     ~MyVector()
  87.     {
  88.         delete[] data;
  89.         //cout << "d'tor" << endl;
  90.     }
  91.     void push_back(int valuve)
  92.     {
  93.         if (size < capacity)
  94.         {
  95.             data[size] = valuve;
  96.             size++;
  97.         }
  98.     }
  99. };
  100.  
  101. void vectorPrinter(MyVector v)
  102. {
  103.     v.print();
  104. }
  105.  
  106. void main()
  107. {
  108.  
  109.  
  110.     MyVector v1(5,2 );
  111.     MyVector v2(5, 3);
  112.     //v1.push_back(1);
  113.     v1.print();
  114.     //vectorPrinter(v);
  115.     //if (v1==v2)
  116.     //{
  117.     //  cout << " WORKS \n";
  118.     //}
  119.     //else
  120.     //{
  121.         //cout << "dont WORKS \n";
  122.  
  123.     //}
  124.  
  125.     v1 = v2;
  126.     v1.print();
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement