Advertisement
35657

Untitled

Mar 14th, 2024
487
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.26 KB | None | 0 0
  1. #include <iostream>
  2.  
  3.  
  4. using namespace std;
  5.  
  6.  
  7. template<typename T>
  8. class Vector {
  9.  
  10. public:
  11.     Vector() : size_(0), capacity_(4), arr_(new T[4]) {}
  12.  
  13.     explicit Vector(const int vector_capacity) : size_(0), capacity_(vector_capacity), arr_(new T[vector_capacity]) {}
  14.  
  15.     Vector(const initializer_list<T>& list) : size_(list.size()), capacity_(list.size()), arr_(new T[list.size()]) {
  16.         int i = 0;
  17.         for (const T& element : list) {
  18.             arr_[i] = element;
  19.             i++;
  20.         }
  21.     }
  22.  
  23.     Vector(const Vector& other) : size_(other.size_), capacity_(other.capacity_), arr_(new T[capacity_]) {
  24.         for (int i = 0; i < size_; i++) {
  25.             arr_[i] = other.arr_[i];
  26.         }
  27.         total_number_elements_ += size_;
  28.     } // конструктор копирования
  29.  
  30.     Vector& operator=(const Vector& other) {
  31.         if (&other != this) { // проверка на самоприсваивание
  32.             total_number_elements_ += other.size_ - size_; // общее количество увеличивается (уменьшается) на разность элементов
  33.             size_ = other.size_;
  34.             capacity_ = other.capacity_;
  35.             delete[] arr_;
  36.             arr_ = new T[capacity_];
  37.             for (int i = 0; i < size_; i++) {
  38.                 arr_[i] = other.arr_[i];
  39.             }
  40.         }
  41.         return *this;
  42.     } // копирующий оператор присваивания =
  43.  
  44.     Vector(Vector&& other) : size_(other.size_), capacity_(other.capacity_), arr_(other.arr_) {
  45.         other.arr_ = nullptr;
  46.         other.size_ = 0;
  47.         other.capacity_ = 0;
  48.     } // конструктор перемещения
  49.  
  50.     Vector& operator=(Vector&& other) {
  51.         if (&other != this) {
  52.             total_number_elements_ += other.size_ - size_; // общее количество увеличивается (уменьшается) на разность элементов
  53.             size_ = other.size_;
  54.             capacity_ = other.capacity_;
  55.             delete[] arr_;
  56.             arr_ = other.arr_;
  57.             other.arr_ = nullptr;
  58.             other.size_ = 0;
  59.             other.capacity_ = 0;
  60.         }
  61.         return *this;
  62.     } // перемещающий оператор присваивания =
  63.  
  64.     void push_back(const T value) {
  65.         check_capacity();
  66.         arr_[size_] = value;
  67.         size_++;
  68.         total_number_elements_++;
  69.     }
  70.  
  71.     void pop_back() {
  72.         if (size_ > 0) {
  73.             size_--;
  74.         }
  75.         total_number_elements_--;
  76.     }
  77.  
  78.     void insert(const int index, const T value) {
  79.         if (index < 0 || index > size_) {
  80.             cout << "Некорректный индекс" << endl;
  81.             return;
  82.         }
  83.         check_capacity();
  84.         for (int i = size_; i > index; i--) {
  85.             arr_[i] = arr_[i - 1];
  86.         }
  87.         arr_[index] = value;
  88.         size_++;
  89.         total_number_elements_++;
  90.     }
  91.  
  92.     void erase(const int index) {
  93.         if (index < 0 || index >= size_) {
  94.             cout << "Некорректный индекс" << endl;
  95.             return;
  96.         }
  97.         for (int i = index; i < size_ - 1; i++) {
  98.             arr_[i] = arr_[i + 1];
  99.         }
  100.         size_--;
  101.         total_number_elements_--;
  102.     }
  103.  
  104.     T& operator[](const int index) {
  105.         if (index < 0 || index >= size_) {
  106.             cout << "Некорректный индекс" << endl;
  107.         }
  108.         return arr_[index];
  109.     }
  110.  
  111.     T operator[](const int index) const {
  112.         if (index < 0 || index >= size_) {
  113.             cout << "Некорректный индекс" << endl;
  114.         }
  115.         return arr_[index];
  116.     }
  117.  
  118.     int size() const {
  119.         return size_;
  120.     }
  121.  
  122.     int capacity() const {
  123.         return capacity_;
  124.     }
  125.  
  126.     bool operator==(const Vector& right) const {
  127.         if (right.size_ != size_) {
  128.             return false;
  129.         }
  130.         for (int i = 0; i < size_; i++) {
  131.             if (arr_[i] != right.arr_[i])
  132.                 return false;
  133.         }
  134.         return true;
  135.     }
  136.  
  137.     bool operator!=(const Vector& right) const {
  138.         return !(*this == right);
  139.     }
  140.  
  141.     void print() const {
  142.         for (int i = 0; i < size_; i++) {
  143.             cout << arr_[i] << " ";
  144.         }
  145.         cout << endl;
  146.     }
  147.  
  148.     static int get_total_number_elements() {
  149.         return total_number_elements_;
  150.     }
  151.  
  152.  
  153.     ~Vector() {
  154.         delete[] arr_;
  155.         total_number_elements_ -= size_;
  156.     }
  157.  
  158. private:
  159.  
  160.     void check_capacity() {
  161.         if (size_ == capacity_) {
  162.             T* temp = new T[capacity_ * 2];
  163.             for (int i = 0; i < size_; i++) {
  164.                 temp[i] = arr_[i];
  165.             }
  166.             delete[] arr_;
  167.             arr_ = temp;
  168.             capacity_ *= 2;
  169.         }
  170.     }
  171.  
  172.     int size_; // текущее количество элементов
  173.     int capacity_; // емкость хранилища
  174.     T* arr_; // хранилище
  175.     static int total_number_elements_; // общее количество элементов по всем векторам
  176. };
  177.  
  178.  
  179. int Vector<int>::total_number_elements_ = 0;
  180. int Vector<string>::total_number_elements_ = 0;
  181.  
  182.  
  183. int main() {
  184.     setlocale(LC_ALL, "ru");
  185.  
  186.     Vector<int> vec1;
  187.  
  188.     vec1.push_back(10);
  189.     vec1.push_back(15);
  190.     vec1.push_back(20);
  191.     vec1.push_back(25);
  192.     vec1.push_back(22);
  193.  
  194.     vec1.print();
  195.  
  196.     vec1.pop_back();
  197.     vec1.print();
  198.  
  199.     vec1.insert(2, 44);
  200.     vec1.print();
  201.  
  202.     vec1.erase(2);
  203.     vec1.print();
  204.  
  205.     vec1[0] = 33;
  206.     cout << vec1[3] << endl;
  207.  
  208.     vec1.print();
  209.  
  210.     cout << vec1.size() << endl;
  211.     cout << vec1.capacity() << endl;
  212.  
  213.     cout << Vector<int>::get_total_number_elements() << endl;
  214.  
  215.     const Vector<int> vec2(vec1);
  216.  
  217.     vec2.print();
  218.  
  219.     cout << vec2.size() << endl;
  220.     cout << vec2.capacity() << endl;
  221.  
  222.     Vector<int> vec3;
  223.     vec3 = vec1;
  224.  
  225.     cout << Vector<int>::get_total_number_elements() << endl;
  226.  
  227.     cout << (vec1 == vec2) << endl;
  228.     cout << (vec1 != vec2) << endl;
  229.  
  230.     Vector<string> vec4{ "один", "два", "три" };
  231.     vec4.print();
  232. }
  233.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement