Advertisement
Galebickosikasa

Untitled

Mar 7th, 2021
883
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.25 KB | None | 0 0
  1. #pragma once
  2.  
  3. #include <iostream>
  4.  
  5. using namespace std;
  6.  
  7. //tg: @runningcherry
  8.  
  9. template <typename T>
  10. class SimpleVector {
  11. public:
  12.     SimpleVector () : size (0), capacity (0) {
  13.         data = nullptr;
  14.     }
  15.  
  16.     explicit SimpleVector (size_t size_) : size (size_), capacity (size_) {
  17.         data = new T[capacity];
  18.     }
  19.  
  20.     ~SimpleVector () {
  21.         delete[] data;
  22.     }
  23.  
  24.     T& operator[] (const size_t index) {
  25.         return data[index];
  26.     }
  27.  
  28.     T* begin () {
  29.         return data;
  30.     }
  31.  
  32.     T* end () {
  33.         return data + size;
  34.     }
  35.  
  36.     const T* begin () const {
  37.         return data;
  38.     }
  39.  
  40.     const T* end () const {
  41.         return data + size;
  42.     }
  43.  
  44.     void PushBack (const T& value) {
  45.         if (size == capacity) {
  46.             capacity <<= 1u;
  47.             if (capacity == 0) capacity = 1;
  48.             T* new_data = new T[capacity];
  49.             for (size_t i = 0; i < size; ++i) new_data[i] = data[i];
  50.             delete[] data;
  51.             data = new_data;
  52.         }
  53.         data[size] = value;
  54.         ++size;
  55.     }
  56.  
  57.     size_t Size () const {
  58.         return size;
  59.     }
  60.  
  61.     size_t Capacity () const {
  62.         return capacity;
  63.     }
  64. private:
  65.     T* data;
  66.     size_t size, capacity;
  67. };
  68.  
  69.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement