Advertisement
deushiro

Untitled

Nov 17th, 2020
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.65 KB | None | 0 0
  1. #include <iostream>
  2.  
  3.  
  4. template<typename Type, size_t size_>
  5. class array{
  6. public:
  7.     typedef Type* pointer;
  8.     typedef Type& ref;
  9.     typedef Type value_type;
  10.  
  11.     template<typename... U>
  12.     array(U... x) : a{x...} {}
  13.  
  14.     ref operator[](size_t index){
  15.         return a[index];
  16.     }
  17.  
  18.     class iterator{
  19.     public:
  20.         iterator(pointer ptr_) : ptr(ptr_){}
  21.         iterator(const iterator& other) = default;
  22.  
  23.         value_type operator[](size_t index) const{
  24.             return ptr[index];
  25.         }
  26.  
  27.         bool operator ==(const iterator& other){
  28.             return(ptr == other.ptr);
  29.         }
  30.         bool operator !=(const iterator& other){
  31.             return(ptr != other.ptr);
  32.         }
  33.         ref operator*() const{
  34.             return *ptr;
  35.         }
  36.         pointer operator->() const{
  37.             return ptr;
  38.         }
  39.         iterator& operator++(){
  40.             ++ptr;
  41.             return *this;
  42.         }
  43.         iterator operator++(int){
  44.             iterator tmp = *this;
  45.             ++(*this);
  46.             return tmp;
  47.         }
  48.         iterator& operator--(){
  49.             --ptr;
  50.             return *this;
  51.         }
  52.         iterator operator--(int){
  53.             iterator tmp = *this;
  54.             --(*this);
  55.             return tmp;
  56.         }
  57.  
  58.  
  59.     private:
  60.         pointer ptr;
  61.     };
  62.  
  63.     iterator begin(){
  64.         return a;
  65.     }
  66.     iterator end(){
  67.         return(&a[size_]);
  68.     }
  69.  
  70. private:
  71.     value_type a[size_];
  72. };
  73.  
  74. int main(){
  75.     int a[3] = {1,2,3};
  76.     array<int,5> arr = {1,2,3,4,5};
  77.     for(auto it = arr.begin(); it != arr.end(); it++){
  78.         std::cout << *it << " ";
  79.     }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement