Advertisement
vencinachev

VectorStructure

Mar 28th, 2021 (edited)
458
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.55 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class myVector
  5. {
  6.     private:
  7.         float *arr;
  8.         int max_size;
  9.         int size;
  10.         int resizeCoeff; // add/remove resizeCoeff elements
  11.         void resize(int option); // option -1 -> decrease, 1 -> increase
  12.     public:
  13.         myVector();
  14.         myVector(int max_size, int resize_coeff);
  15.         myVector(float *a, int n);
  16.         myVector(myVector *other);
  17.         ~myVector();
  18.         int count();
  19.         int capacity();
  20.         void insert(int pos, float el);
  21.         void push_front(float el);
  22.         void push_back(float el);
  23.         float remove(int pos);
  24.         float remove_front();
  25.         float remove_back();
  26.         float at(int index);
  27. };
  28.  
  29. void myVector::resize(int option)
  30. {
  31.     this->max_size += (option * this->resizeCoeff);
  32.     float *newArr = new float[this->max_size];
  33.     for (int i = 0; i < this->size; i++)
  34.     {
  35.         newArr[i] = this->arr[i];
  36.     }
  37.     delete[] arr;
  38.     this->arr = newArr;
  39. }
  40. myVector::myVector()
  41. {
  42.     this->arr = new float[10];
  43.     this->max_size = 10;
  44.     this->size = 0;
  45.     this->resizeCoeff = 5;
  46. }
  47. myVector::myVector(int max_size, int resize_coeff)
  48. {
  49.     this->arr = new float[max_size];
  50.     this->max_size = max_size;
  51.     this->size = 0;
  52.     this->resizeCoeff = resize_coeff;
  53. }
  54. myVector::myVector(float *a, int n)
  55. {
  56.     this->arr = new float[n + 10];
  57.     for (int i = 0; i < n; i++)
  58.     {
  59.         this->arr[i] = a[i];
  60.     }
  61.     this->max_size = n + 10;
  62.     this->size = n;
  63. }
  64. myVector::myVector(myVector *other)
  65. {
  66.     this->arr = new float[other->capacity()];
  67.     for (int i = 0; i < other->count(); i++)
  68.     {
  69.         this->arr[i] = other->at(i);
  70.     }
  71.     this->max_size = other->capacity();
  72.     this->size = other->count();
  73. }
  74.  
  75. myVector::~myVector()
  76. {
  77.     delete[] arr;
  78. }
  79.  
  80. int myVector::count()
  81. {
  82.     return this->size;
  83. }
  84.  
  85. int myVector::capacity()
  86. {
  87.     return this->max_size;
  88. }
  89.  
  90. void myVector::push_back(float el)
  91. {
  92.     insert(this->size, el);
  93. }
  94.  
  95. void myVector::insert(int pos, float el)
  96. {
  97.     if (this->size == this->max_size)
  98.     {
  99.         this->resize(1);
  100.     }
  101.     for (int i = size; i > pos; i--)
  102.     {
  103.         this->arr[i] = this->arr[i-1];
  104.     }
  105.     this->arr[pos] = el;
  106.     this->size++;
  107. }
  108.  
  109. void myVector::push_front(float el)
  110. {
  111.     this->insert(0, el);
  112. }
  113.  
  114. float myVector::remove(int pos){
  115.     int el = this->arr[pos];
  116.     for (int i = pos; i < this->size - 1; i++)
  117.     {
  118.         this->arr[i] = this->arr[i+1];
  119.     }
  120.     this->size--;
  121.     if (this->size + this->resizeCoeff < this->max_size)
  122.     {
  123.         this->resize(-1);
  124.     }
  125.     return el;
  126. }
  127.  
  128. float myVector::remove_front()
  129. {
  130.     return remove(0);
  131. }
  132.  
  133. float myVector::remove_back()
  134. {
  135.     return remove(this->size - 1);
  136. }
  137.  
  138. float myVector::at(int index)
  139. {
  140.     if (index < 0 || index >= this->size)
  141.     {
  142.         throw (index);
  143.     }
  144.     return this->arr[index];
  145. }
  146.  
  147. int main()
  148. {
  149.     myVector v1;
  150.     for(int i = 0; i <= 11; i++)
  151.     {
  152.         v1.push_back(i);
  153.     }
  154.     v1.insert(5, 19);
  155.     v1.remove(3);
  156.     v1.remove_front();
  157.     v1.push_front(31);
  158.     v1.remove_back();
  159.     cout << "V1: Capacity: " << v1.capacity() << " Count: " << v1.count();
  160.     cout << " Elements: ";
  161.     for(int i = 0; i < v1.count(); i++)
  162.     {
  163.         cout << v1.at(i) << " ";
  164.     }
  165.     cout << endl;
  166.  
  167.     float nums[] = {10, 20, 30, 40, 50};
  168.     myVector v2(nums, 5);
  169.     cout << "V2: Capacity: " << v2.capacity() << " Count: " << v2.count();
  170.     cout << " Elements: ";
  171.     for(int i = 0; i < v2.count(); i++)
  172.     {
  173.         cout << v2.at(i) << " ";
  174.     }
  175.     cout << endl;
  176.  
  177.     myVector v3(&v2);
  178.     cout << "V3: Capacity: " << v3.capacity() << " Count: " << v3.count();
  179.     cout << " Elements: ";
  180.     for(int i = 0; i < v3.count(); i++)
  181.     {
  182.         cout << v3.at(i) << " ";
  183.     }
  184.     cout << endl;
  185.  
  186.     myVector v4(20, 10);
  187.     for(int i = 1; i <= 21; i++){
  188.         v4.push_back(i);
  189.     }
  190.     cout << "V4: Capacity: " << v4.capacity() << " Count: " << v4.count();
  191.     cout << " Elements: ";
  192.     for(int i = 0; i < v4.count(); i++)
  193.     {
  194.         cout << v4.at(i) << " ";
  195.     }
  196.     cout << endl;
  197.  
  198.     try
  199.     {
  200.         float num = v1.at(-19);
  201.         cout << num;  // not print
  202.     }
  203.     catch (int index)
  204.     {
  205.         cout << index << " is invalid index!" << endl;
  206.     }
  207.    
  208.     try
  209.     {
  210.         float num = v1.at(23);
  211.         cout << num;  // not print
  212.     }
  213.     catch (int index)
  214.     {
  215.         cout << index << " is invalid index!" << endl;
  216.     }
  217.     return 0;
  218. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement