Advertisement
Guest User

Untitled

a guest
Dec 8th, 2019
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.28 KB | None | 0 0
  1. template <typename T, std::size_t N>
  2. class Array {
  3.    
  4.     friend bool operator==(const Array& lhs, const Array& rhs) {
  5.         return (lhs.size() == rhs.size()) && equal(lhs.begin(), lhs.end(), rhs.begin());
  6.     }
  7.    
  8.     // using rel_ops::operator!=; // could replace this
  9.     friend bool operator!=(const Array& lhs, const Array& rhs) {
  10.         return !(lhs==rhs);
  11.     }
  12.    
  13. private:
  14.    
  15.     T a[N];
  16.  
  17. public:
  18.     Array() {}
  19.    
  20.     Array(const initializer_list<T>& rhs) {
  21.         if (rhs.size() != 0) {
  22.             copy(rhs.begin(), rhs.end(), begin());
  23.         }
  24.     }
  25.    
  26.     T& operator[](T i) {
  27.         return a[i];
  28.     }
  29.     const T& operator[](T i) const {
  30.         return (*const_cast<Array*>(this))[i];
  31.     }
  32.  
  33.     Array& operator=(const Array& rhs) {
  34.         if (this == &rhs){
  35.             return *this;
  36.         }
  37.         if (rhs.size() != 0) {
  38.             copy(rhs.begin(), rhs.end(), begin());
  39.         }
  40.         return *this;
  41.     }
  42.  
  43.     T* begin() {
  44.         return &a[0];
  45.     }
  46.     const T* begin() const {
  47.         return const_cast<Array*>(this)->begin();
  48.     }
  49.  
  50.     T* end() {
  51.         return &a[N];
  52.     }
  53.     const T* end() const {
  54.         return const_cast<Array*>(this)->end();
  55.     }
  56.    
  57.     int size() const {
  58.         return N;
  59.     }
  60.  
  61. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement