Skip to content
Snippets Groups Projects
myarray.hpp 2.33 KiB
Newer Older
  • Learn to ignore specific revisions
  • Martin Weinelt's avatar
    Martin Weinelt committed
    //
    // Created by hexa on 20.11.21.
    //
    
    #include <iostream>
    #include <initializer_list>
    
    template< class T, int N > class myArray {
    public:
        myArray();
        explicit myArray(const T&);
        myArray(const myArray&);
        myArray(std::initializer_list<T>);
        myArray &operator=(const myArray &);
        T *data(int);
        T &operator[](int);
        T &at(int);
        const T& operator[](int) const;
        const T& at(int) const;
        int size() const { return N; }
        void fill(const T&);
        void printAll() const;
    
    private:
        T elem[N];
    };
    
    using std::cout;
    using std::endl;
    
    template<class T, int N>
    T &myArray<T, N>::operator[](int n) {
        return elem[n];
    }
    
    template<class T, int N>
    T &myArray<T, N>::at(int n) {
        if (n < 0 || N <= n) {
            throw std::out_of_range{"myArray<T,N>::at: index out of range"};
        }
        return elem[n];
    }
    
    template<class T, int N>
    const T &myArray<T, N>::operator[](int n) const {
        return elem[n];
    }
    
    template<class T, int N>
    const T &myArray<T, N>::at(int n) const {
        if (n < 0 || N <= n) {
            throw std::out_of_range{"myArray<T,N>::at(const): index out of range"};
        }
        return elem[n];
    }
    
    template<class T, int N>
    T *myArray<T, N>::data(int n) {
        return &(elem[n]);
    }
    
    template<class T, int N>
    myArray<T, N>::myArray() : myArray{T{}} {}
    
    template<class T, int N>
    myArray<T, N>::myArray(const T &t) {
        for (int i{}; i < N; ++i) {
            elem[i] = t;
        }
    }
    
    template<class T, int N>
    myArray<T, N> &myArray<T, N>::operator=(const myArray<T, N> &r) {
        if (this == &r) {
            return *this;
        }
        for (int i{}; i < N; ++i) elem[i] = r.elem[i];
        return *this;
    }
    
    // Copy
    template<class T, int N>
    myArray<T, N>::myArray(const myArray<T, N> &other) {
        *this = other;
    }
    
    // Konstruktor mit Initializer List
    template<class T, int N>
    myArray<T, N>::myArray(std::initializer_list<T> in) {
        std::copy(in.begin(), in.end(), elem);
    }
    
    template<class T, int N>
    void myArray<T, N>::printAll() const {
        for (int i{}; i < size(); ++i) {
            cout << i << ") " << &(elem[i]) << " = " << elem[i] << endl;
        }
    }
    
    // Fill (Member)
    template<class T, int N>
    void myArray<T, N>::fill(const T &t) {
        for (unsigned int i{}; i < N; ++i) {
            elem[i] = t;
        }
    }
    
    // Fill (Helper)
    template<class T, int N>
    void fill(myArray<T, N> &a, const T &val) {
        for (unsigned int i{}; i < N; ++i) {
            a[i] = val;
        }
    }