fork download
  1. /** Interface for MyVector class
  2. **/
  3. #include <cstdlib>
  4. template <typename T> class MyVector {
  5.  
  6. private:
  7. static int const INITIAL_CAPACITY = 10;
  8. int _size;
  9. int _capacity;
  10. T* data;
  11. void reallocate();
  12.  
  13. public:
  14. MyVector();
  15. MyVector(int capacity);
  16. ~MyVector();
  17. void push_back(T const& entry);
  18. bool remove(int index);
  19. T get(int index);
  20. int size();
  21. int capacity();
  22. bool empty();
  23. void insert(T const& entry, unsigned int index);
  24.  
  25. //Overloaded operators
  26. T& operator[](const unsigned int index);
  27. const T& operator[](const unsigned int index) const;
  28.  
  29. };
  30.  
  31. /** Definition of MyVector class
  32. **/
  33. template <typename T>
  34. MyVector<T>::MyVector()
  35. {
  36. _capacity = INITIAL_CAPACITY;
  37. data = (T*)std::malloc(_capacity * sizeof(T));//data = new T[_capacity];
  38. _size = 0;
  39. }
  40.  
  41. template <typename T>
  42. MyVector<T>::MyVector(int capacity)
  43. {
  44. if(capacity <= 0)
  45. {
  46. _capacity = INITIAL_CAPACITY;
  47. }
  48. else
  49. {
  50. _capacity = capacity;
  51. }
  52. data = new T[_capacity];
  53. _size = 0;
  54. }
  55.  
  56. template <typename T>
  57. MyVector<T>::~MyVector()
  58. {
  59. std::free(data);//delete[] data;
  60. data = 0;
  61. }
  62.  
  63. template <typename T>
  64. int MyVector<T>::size()
  65. {
  66. return _size;
  67. }
  68.  
  69. template <typename T>
  70. void MyVector<T>::reallocate()
  71. {
  72. _capacity = _capacity * 2;
  73. T* temp = (T*)std::malloc(_capacity * sizeof(T));
  74. for(int i = 0; i < _size; i++)
  75. {
  76. temp[i] = data[i];
  77. }
  78.  
  79. std::free(data);//delete[] data;
  80. data = temp;
  81.  
  82. //std::cout << "Reallocation successful.\n";
  83. }
  84.  
  85. template <typename T>
  86. void MyVector<T>::push_back(T const& entry)
  87. {
  88. if(_size == _capacity)
  89. {
  90. reallocate();
  91. }
  92.  
  93. data[_size] = entry;
  94. _size++;
  95. }
  96.  
  97. template <typename T>
  98. bool MyVector<T>::remove(int index)
  99. {
  100. if(index < 0 || index >= _size)
  101. {
  102. return false;
  103. }
  104. else
  105. {
  106. for(int i = index; i < _size; i++)
  107. {
  108. data[i] = data[i + 1];
  109. }
  110. }
  111. _size--;
  112. return true;
  113. }
  114.  
  115. template <typename T>
  116. T MyVector<T>::get(int index)
  117. {
  118. return data[index];
  119. }
  120.  
  121. template <typename T>
  122. T& MyVector<T>::operator[](const unsigned int index)
  123. {
  124. return data[index];
  125. }
  126.  
  127. template <typename T>
  128. const T& MyVector<T>::operator[](const unsigned int index) const
  129. {
  130. return data[index];
  131. }
  132.  
  133. template <typename T>
  134. int MyVector<T>::capacity()
  135. {
  136. return _capacity;
  137. }
  138.  
  139. template <typename T>
  140. bool MyVector<T>::empty()
  141. {
  142. if(_size > 0)
  143. {
  144. return false;
  145. }
  146. else
  147. {
  148. return true;
  149. }
  150. }
  151.  
  152. template <typename T>
  153. void MyVector<T>::insert(T const& entry, unsigned int index)
  154. {
  155. if(_size + 1 > _capacity)
  156. {
  157. reallocate();
  158. }
  159. for(unsigned int i = _size; i > index; i--)
  160. {
  161. data[i] = data[i - 1];
  162. }
  163.  
  164. data[index] = entry;
  165. }
  166.  
  167. #include <iostream>
  168. #include <vector>
  169.  
  170. struct pair
  171. {
  172. int x;
  173. int y;
  174.  
  175. pair(int x, int y) : x(x), y(y) {}
  176. };
  177.  
  178. int main()
  179. {
  180. MyVector<pair> v;
  181. //std::vector<pair> v;
  182.  
  183. for(int i = 0; i < 10; ++i)
  184. {
  185. v.push_back(pair(i,2*i));
  186. }
  187.  
  188. for(int i = 0; i < 10; ++i)
  189. {
  190. std::cout << v[i].x << " " << v[i].y << std::endl;
  191. }
  192. }
Success #stdin #stdout 0s 3028KB
stdin
Standard input is empty
stdout
0   0
1   2
2   4
3   6
4   8
5   10
6   12
7   14
8   16
9   18