Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2018
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.08 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. template <typename T>
  6. class Vector
  7. {
  8. private:
  9. T* _array;
  10. int _size;
  11. int capacity;
  12.  
  13. public:
  14. Vector ()
  15. {
  16. _array = new T[1];
  17. _size = 0;
  18. capacity = 1;
  19. }
  20.  
  21. void reserve(int new_capacity)
  22. {
  23. T* new_array = new T[new_capacity];
  24. for (int i = 0; i < _size; i++)
  25. {
  26. new_array[i] = _array[i];
  27.  
  28. }
  29. capacity = new_capacity;
  30. delete [] _array;
  31. _array = new_array;
  32. }
  33.  
  34. void push_back(T element)
  35. {
  36. if(_size == capacity)
  37. reserve(_size*2);
  38. _array[_size ++] = element;
  39. }
  40. void get_resize(int n)
  41. {
  42. if (capacity < n ) _size = n;
  43. else
  44. {
  45. int t;
  46. t = n;
  47. capacity = t*2;
  48. _size=n;
  49. }
  50. }
  51. T& operator[](int n)
  52. {
  53. return _array[n];
  54. }
  55.  
  56. void insert(int n, T element)
  57. {
  58. if (_size == capacity) reserve(_size*2);
  59.  
  60. for (int i = _size; i > n; i--)
  61. _array[i] = _array[i-1];
  62.  
  63. _array[n] = element;
  64.  
  65. }
  66.  
  67. void clear()
  68. {
  69. delete []_array;
  70. _array = new T[1];
  71. _size = 0;
  72. capacity = 1;
  73. }
  74.  
  75. void erase(int n)
  76. {
  77. for (int i = n; i < (_size-1); i++)
  78. {
  79. _array[i]=_array[i+1];
  80.  
  81. }
  82. _size--;
  83. }
  84.  
  85. void pop_back()
  86. {
  87. _size--;
  88. }
  89.  
  90. void swap(int a, int b)
  91. {
  92. T temp = _array[a];
  93. _array[a] =_array[b];
  94. _array[b] = temp;
  95.  
  96. }
  97.  
  98. int getSize()
  99. {
  100. return _size;
  101. }
  102.  
  103. int getCapacity()
  104. {
  105. return capacity;
  106. }
  107.  
  108. bool empty()
  109. {
  110. return (_size == 0);
  111. }
  112. T& it_begin()
  113. {
  114. return _array[0];
  115.  
  116. }
  117. T& it_end()
  118. {
  119. return _array[_size];
  120.  
  121. }
  122. };
  123.  
  124. int main()
  125. { setlocale(LC_CTYPE, "rus");
  126. Vector<int> vector_;
  127. cout<<"Size:";
  128. int _size;
  129. cin>>_size;
  130. cout<<"Elements:"<<endl;
  131. int element;
  132. for(int i=0;i< _size; i++)
  133. {
  134. cin>>element;
  135. vector_.push_back(element);
  136. }
  137. vector_.get_resize(8);
  138. for (int i =0;i<vector_.getSize();i++) {
  139. cout <<vector_[i];
  140. cout<<endl;
  141. }
  142. cout << "Size:" << vector_.getSize();
  143. /* cout << endl;
  144. cout << "Size:" << vector_.getSize() << " " << "Empty?" << vector_.empty() << endl;
  145. cout<<"Clear vector"<<endl;
  146. vector_.clear();
  147. cout<<"Empty?"<<vector_.empty()<<endl;
  148. cout<<"Add 1"<<endl;
  149. cout<<"Add 2"<<endl;
  150. cout<<"Add 3"<<endl;
  151. vector_.push_back(1);
  152. vector_.push_back(2);
  153. vector_.push_back(3);
  154. cout<<"Swap index 0 and index 2 "<<endl;
  155. vector_.swap(0,2);
  156. cout<<"Delete inlex 1"<<endl;
  157. vector_.erase(1);
  158. cout<<"Vector:"<<endl;
  159. for (int i =0;i<vector_.getSize();i++) {
  160. cout <<vector_[i];
  161. cout<<endl;
  162. }
  163. cout << vector_.it_begin();
  164. cout << vector_.it_end();
  165. */return 0;
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement