SHARE
TWEET

Untitled

a guest Apr 24th, 2019 63 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "pch.h"
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. template <typename T>
  6. class SimpleVector
  7. {
  8. public:
  9.     /*          Пока не имеет смысла.
  10.     SimpleVector()
  11.     {
  12.         data = nullptr;
  13.         last = data;
  14.         end_ = data + 1;
  15.     }
  16.     */
  17.     SimpleVector(size_t size)
  18.     {
  19.         data = new T[size];
  20.         last = data;
  21.         end_ = data + size;
  22.     }
  23.  
  24.     ~SimpleVector()
  25.     {
  26.         delete[] data;
  27.     }
  28.    
  29.     const T& operator[] (size_t index)
  30.     {
  31.         return data[index];
  32.     }
  33.  
  34.     T* begin() { return data; }
  35.     T* end() { return end_; }
  36.  
  37.     const T* begin() const { return data; }
  38.     const T* end() const { return end_; }
  39.  
  40.     size_t Capacity() const
  41.     {
  42.         return end_ - data;
  43.     }
  44.    
  45.     size_t Size() const
  46.     {
  47.         return last - data;
  48.     }
  49.    
  50.     void PushBack(const T& value)
  51.     {
  52.         *last = value;
  53.         last++;
  54.     };
  55.    
  56.  
  57. private:
  58.     T* data;
  59.     T* last;
  60.     T* end_;
  61. };
  62.  
  63.  
  64. int main()
  65. {
  66.     SimpleVector<int> vec(10);
  67.     for (int i = 0; i < 10; i++)
  68.     {
  69.         vec.PushBack(i * 5);
  70.     }
  71.     for (const auto& i : vec)
  72.     {
  73.         cout << i << " ";
  74.     }
  75. }
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