Guest User

Untitled

a guest
Sep 23rd, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.98 KB | None | 0 0
  1. void PushBack(const T& value) {
  2. T* result;
  3.  
  4. if (Array == nullptr) {
  5. result = new T[1];
  6. Array = result;
  7. end_ = Array + 1;
  8. }
  9. else if (arSize == (*this).Capacity()) {
  10. size_t capacity = (*this).Capacity() * 2;
  11. T* result = new T[capacity];
  12.  
  13.  
  14. for (unsigned int i = 0; i < arSize; ++i)
  15. result[i] = Array[i];
  16.  
  17. Array = result;
  18. end_ = Array + capacity;
  19.  
  20. }
  21.  
  22. Array[arSize++] = value;
  23. }
  24.  
  25. private:
  26. T* Array;
  27. T* end_;
  28. unsigned int arSize = 0;
  29.  
  30. `template <typename T>
  31. class SimpleVector {
  32. public:
  33. SimpleVector() {
  34. Array = nullptr;
  35. end_ = Array;
  36. }
  37.  
  38. explicit SimpleVector(size_t size) : Array(new T[size]), end_(Array + size){
  39. arSize += size;
  40. }
  41.  
  42. ~SimpleVector() {
  43. delete[] Array;
  44. }
  45.  
  46. T& operator[](size_t index) {
  47. return Array[index];
  48. }
  49.  
  50. T* begin() {
  51. return Array;
  52. }
  53.  
  54. T* end() {
  55. return end_;
  56. }
  57.  
  58. size_t Size() const {
  59. return arSize;
  60. }
  61.  
  62. size_t Capacity() const {
  63. return end_ - Array;
  64. }
  65.  
  66. void PushBack(const T& value) {
  67. T* result;
  68.  
  69. if (Array == nullptr) {
  70. result = new T[1];
  71. Array = result;
  72. end_ = Array + 1;
  73. }
  74. else if (arSize == (*this).Capacity()) {
  75. size_t capacity = (*this).Capacity() * 2;
  76. T* result = new T[capacity];
  77.  
  78.  
  79. for (unsigned int i = 0; i < arSize; ++i)
  80. result[i] = Array[i];
  81.  
  82. Array = result;
  83. end_ = Array + capacity;
  84.  
  85. }
  86.  
  87. Array[arSize++] = value;
  88. }
  89.  
  90. private:
  91. T* Array;
  92. T* end_;
  93. unsigned int arSize = 0;
  94.  
  95. };`
  96.  
  97. void PushBack(const T& value) {
  98. T* result;
  99.  
  100. if (Array == nullptr) {
  101. result = new T[1];
  102.  
  103. else if (arSize == (*this).Capacity()) {
  104. size_t capacity = (*this).Capacity() * 2;
  105. T* result = new T[capacity];
Add Comment
Please, Sign In to add comment