Advertisement
Timtsa

lesson

Nov 29th, 2018
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.08 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.     void print()
  31.     {
  32.         for (size_t i = 0; i < size; i++)
  33.         {
  34.             cout << data[i] << ' ';
  35.         }
  36.    
  37.    
  38.     }
  39.     MyVector(const MyVector &other)
  40.     {
  41.         this->size = other.size;
  42.         this->capacity = other.capacity;
  43.         this->data = new int[this->capacity];
  44.         copy(other.data, other.data + capacity, this->data);
  45.     }
  46.    
  47.    
  48.    
  49.     ~MyVector()
  50.     {
  51.         delete[] data;
  52.         //cout << "d'tor" << endl;
  53.     }
  54.     void push_back(int valuve)
  55.     {
  56.         if (size < capacity)
  57.         {
  58.             data[size] = valuve;
  59.             size++;
  60.         }
  61.     }
  62. };
  63.  
  64. void vectorPrinter(MyVector v)
  65. {
  66.     v.print();
  67. }
  68.  
  69. void main()
  70. {
  71.  
  72.  
  73.     MyVector v(5,2 );
  74.     v.push_back(1);
  75.     v.print();
  76.     //vectorPrinter(v);
  77.  
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement