Advertisement
RobertDeMilo

RB3.8 Добавляем в вектор begin и end

Apr 16th, 2024
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.80 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <vector>
  4.  
  5. using namespace std;
  6.  
  7. template <typename T>
  8. class SimpleVector
  9. {
  10. public:
  11.     explicit SimpleVector(size_t size)
  12.     {
  13.         data = new T[size];
  14.         end_ = data + size;
  15.     }
  16.    
  17.     ~SimpleVector()
  18.     {
  19.         delete[]data;
  20.     }
  21.    
  22.     T& operator[](size_t index)
  23.     {
  24.         //return *(data + index);
  25.         return data[index];
  26.     }
  27.    
  28.     T* begin()
  29.     {
  30.         return data;
  31.     }
  32.    
  33.     T* end()
  34.     {
  35.         return end_;
  36.     }
  37.    
  38. private:
  39.     T* data;
  40.     T* end_;
  41. };
  42.  
  43. int main()
  44. {
  45.     SimpleVector<int> sv(4);
  46.     sv[0] = 5;
  47.     sv[1] = 3;
  48.     sv[2] = 4;
  49.     sv[3] = -1;
  50.  
  51.     sort(sv.begin(), sv.end());
  52.    
  53.     for (int x : sv)
  54.     {
  55.         cout << x << ' ';
  56.     }
  57. }
  58.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement