SHARE
TWEET

Untitled

a guest Mar 24th, 2019 66 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "pch.h"
  2. #include <iostream>
  3. #include <memory.h>
  4.  
  5. using T = int;
  6.  
  7. using namespace ::std;
  8.  
  9. class Vector
  10. {
  11. public:
  12.     Vector();
  13.     Vector(unsigned size1);
  14.     Vector(unsigned size1, const T& val);
  15.     Vector(const Vector&Vector1);
  16.  
  17.     Vector& operator =(const Vector&Vector1);
  18.  
  19.     void resize(unsigned size1);
  20.     void push_back(const T& val);
  21.  
  22.     bool insert(unsigned pos, const T& val);   
  23.     bool contains(const T& val);
  24.  
  25.     int find(const T& val);
  26.  
  27.     T& operator [](unsigned index);
  28.     T* data1();
  29.  
  30.     unsigned Size();
  31.  
  32.     ~Vector();
  33.  
  34. private:
  35.     T*data;
  36.     unsigned size;
  37. };
  38.  
  39. Vector::Vector() // defualt constructor
  40. {
  41.     data = nullptr;
  42.  
  43.     size = 0;
  44. }
  45.  
  46. Vector::Vector (unsigned Size1) //mass. with defualt value
  47. {
  48.     data = new T[size];
  49.  
  50.     for (unsigned i = 0; i < size; i++)
  51.     {
  52.         data[i] = T();
  53.     }
  54. }
  55.  
  56. Vector::Vector(unsigned  size1, const T& val) //mass. with val value
  57. {
  58.     data = new T[size];
  59.  
  60.     for (unsigned i = 0; i < size; i++)
  61.     {
  62.         data[i] = val;
  63.     }
  64. }
  65.  
  66. Vector::Vector(const Vector&Vector1)
  67. {
  68.     size = Vector1.size;
  69.     data = new T[size];
  70.  
  71.     memcpy(data, Vector1.data, sizeof(T)*size);
  72. }
  73.  
  74. unsigned Vector::Size() //return value of mass.
  75. {
  76.     return size;
  77. }
  78.  
  79. void Vector::resize(unsigned size1)
  80. {
  81.     T*buff = data;
  82.    
  83.     if (size1 > size)
  84.     {
  85.         buff = new T[size1];
  86.  
  87.         memcpy(buff, data, sizeof(T)*size);
  88.  
  89.         for (unsigned i = 0; i < size1; i++)
  90.         {
  91.             buff[i] = 0;
  92.         }
  93.  
  94.         size = size1;
  95.     }
  96.     else
  97.     {
  98.         buff = new T[size];
  99.  
  100.         memcpy(buff, data, sizeof(T)*size1);
  101.     }
  102.  
  103.     delete[]data;
  104.     data = buff;
  105.     size = size1;
  106. }
  107.  
  108. bool Vector::insert(unsigned pos, const T& val)
  109. {
  110.     if (pos <= size)
  111.     {
  112.         resize(size + 1);
  113.  
  114.         for (unsigned i = pos; i < size; ++i)
  115.         {
  116.             data[i] = data[i - 1];
  117.         }
  118.  
  119.         data[pos-1] = val;
  120.  
  121.         return (true);
  122.     }
  123.     else
  124.     {
  125.         return (false);
  126.     }
  127. }
  128.  
  129. void Vector::push_back(const T& val)
  130. {
  131.     insert(size, val);
  132. }
  133.  
  134. bool Vector::contains(const T& val)
  135. {
  136.     for (unsigned i = 0; i < size; i++)
  137.     {
  138.         if (data[i] == val)
  139.         {
  140.             return (true);
  141.         }
  142.     }
  143.  
  144.     return (false);
  145. }
  146.  
  147. T& Vector::operator [](unsigned index)
  148. {
  149.     return data[index];
  150. }
  151.  
  152. int Vector::find(const T& val)
  153. {
  154.     for (unsigned i = 0; i < size; ++i)
  155.     {
  156.         if (val == data[i])
  157.         {
  158.             return (i);
  159.         }
  160.  
  161.     }
  162.             return (-1);
  163. }
  164.  
  165. Vector& Vector::operator =(const Vector&Vector1)
  166. {
  167.     if (this != &Vector1)
  168.     {
  169.         size = Vector1.size;
  170.  
  171.         delete[]data;
  172.         data = new T[size];
  173.  
  174.         memcpy(data, Vector1.data, sizeof(T)*size);
  175.     }
  176.     return *this;
  177. }
  178.  
  179. T* Vector::data1()
  180. {
  181.     return (data);
  182. }
  183.  
  184. Vector::~Vector()
  185. {
  186.     size = 0;
  187.     delete[]data;
  188. }
  189.  
  190. int main()
  191. {
  192.     Vector Vector1(7);
  193.    
  194.     Vector1.resize(5);
  195.  
  196.     cout << "Actual size of Vector1: " << Vector1.Size() << endl;
  197.     cout << endl;
  198.  
  199.     Vector1.insert(11, 1);
  200.  
  201.     cout << "Try to find '1' in Vector1: " << Vector1.find(1) << endl;
  202.     cout << endl;
  203.  
  204.     Vector1.push_back(69);
  205.     cout << "Try to find 69 in Vector1: " << Vector1.contains(69) << endl;
  206.     cout << endl;
  207.  
  208.  
  209.     cout << "Get Data: " << Vector1.data1() << endl;
  210. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top