Advertisement
coloriot

HA24

Sep 20th, 2024 (edited)
20
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.54 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. // Пишем общий класс векторов
  6. // Иногда добавляем reinterpret_cast
  7. // Довольно часто руками вызываю ~T() - деструктор T
  8.  
  9. template <typename T>
  10. class Vector {
  11.     T* array;
  12.     int size;
  13.     int capacity;
  14. public:
  15.  
  16.     Vector(){
  17.         array = nullptr;
  18.         size = 0;
  19.         capacity = 0;
  20.     }
  21.  
  22.     Vector(const Vector& other){
  23.         size = other.size;
  24.         capacity = other.capacity;
  25.         array = reinterpret_cast<T*>(new char[capacity * sizeof(T)]);
  26.         for (int i = 0; i < size; ++i){
  27.             new (array[i]+1) T(other.array[i]);
  28.         }
  29.     }
  30.  
  31.     Vector& operator=(const Vector& other){
  32.         if (this != &other){
  33.             for (int i = 0; i < size; ++i) {
  34.                 array[i].~T();
  35.             }
  36.             delete[] reinterpret_cast<char*>(array);
  37.             size = other.size;
  38.             capacity = other.capacity;
  39.             array = reinterpret_cast<T*>(new char[capacity * sizeof(T)]);
  40.             for (int i =  0; i < size; ++i){
  41.                 new(array + i) T(other.array[i]);
  42.             }
  43.         }
  44.         return *this;
  45.     }
  46.  
  47.     T& operator[](int index){
  48.         return array[index];
  49.     }
  50.     const T& operator[](int index) const{
  51.         return array[index];
  52.     }
  53.     int getSize() const{
  54.         return size;
  55.     }
  56.     int getCapacity() const{
  57.         return capacity;
  58.     }
  59.  
  60.     void push_back(const T& value){
  61.         if (size == capacity){
  62.             if (capacity == 0){
  63.                 reserve(1);
  64.             } else {
  65.                 reserve (capacity * 2);
  66.             }
  67.         }
  68.         new (array + size) T(value);
  69.         ++size;
  70.     }
  71.     void pop_back(){
  72.         if (size > 0){
  73.             array[size -1].~T();
  74.             --size;
  75.         }
  76.     }
  77.  
  78.     // Ненаивный reserve
  79.     void reserve(int new_capacity) {
  80.         T* new_array =  reinterpret_cast<T*>(new char[new_capacity* sizeof(T)]);
  81.         for (int i = 0; i < size; ++i){
  82.             new (new_array + i) T(array[i]);
  83.         }
  84.         delete[] array;
  85.         array = new_array;
  86.         capacity = new_capacity;
  87.     }
  88.  
  89.     // Чуть измененный resize
  90.     void resize(int new_size, const T& value){
  91.         if (new_size > capacity){
  92.             reserve(new_size);
  93.         }
  94.         for (int i = size; i < new_size; ++i){
  95.             new (array + i) T(value);
  96.         }
  97.         for (int i = new_size; i < size; ++i) {
  98.             array[i].~T();
  99.         }
  100.         size = new_size;
  101.     }
  102.     ~Vector() {
  103.         for (int i = 0; i < size; ++i) {
  104.             array[i].~T();
  105.         }
  106.         delete[] reinterpret_cast<char*>(array);
  107.     }
  108. };
  109.  
  110. int main() {
  111.     Vector<int> vec;
  112.  
  113.     vec.push_back(1);
  114.     vec.push_back(2);
  115.     vec.push_back(3);
  116.  
  117.     cout << "Размер: " << vec.getSize() << endl;
  118.     cout << "Объем: " << vec.getCapacity() << endl;
  119.  
  120.     cout << "Элемент по индексу 0: " << vec[0] << endl;
  121.     cout << "Элемент по индексу 1: " << vec[1] << endl;
  122.     cout << "Элемент по индексу 0: " << vec[2] << endl;
  123.  
  124.     vec.pop_back();
  125.     cout << "Размер после pop_back: " << vec.getSize() << endl;
  126.  
  127.     vec.resize(5, 10);
  128.     cout << "Размер после ресайза: " << vec.getSize() << endl;
  129.     cout << "Элемент по индексу 3: " << vec[3] << endl;
  130.     cout << "Элемент по индексу 3: " << vec[4] << endl;
  131.  
  132.     return 0;
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement