| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- /* ************************************************************************** */
- /* */
- /* ::: :::::::: */
- /* Array.hpp :+: :+: :+: */
- /* +:+ +:+ +:+ */
- /* By: bchanot <bchanot@42.fr> +#+ +:+ +#+ */
- /* +#+#+#+#+#+ +#+ */
- /* Created: 2026/01/11 15:29:11 by bchanot #+# #+# */
- /* Updated: 2026/01/11 16:52:20 by bchanot ### ########.fr */
- /* */
- /* ************************************************************************** */
- #ifndef ARRAY_HPP
- # define ARRAY_HPP
- # include <iostream>
- # include <string>
- template <typename T>
- class Array
- {
- public:
- Array() { this->_elements = new T[]; };
- Array(unsigned int const n) : _size(n), _elements(new T[n]) {};
- Array( Array const & src ) : _size(src.size()), _elements(new T[this->_size]) {
- size_t fill = 0;
- while (fill < this->_size) {
- this->_elements[fill] = src[fill];
- fill++;
- }
- };
- ~Array() { delete[] this->_elements; };
- class IndexOutOfBounds : public std::exception {
- public:
- virtual const char *what() const throw() {
- return ("The index specified is out of bounds !");
- }
- };
- Array & operator=( Array const & rhs ) {
- if (rhs == *this)
- return *this;
- size_t fill = 0;
- this->_size = rhs.size();
- this->_elements = new T[this->_size];
- while (fill < this->_size) {
- this->_elements[fill] = rhs[fill];
- fill++;
- }
- return *this;
- };
- T &operator[](size_t idx) {
- if (idx >= this->_size)
- throw IndexOutOfBounds();
- return this->_elements[idx];
- };
- T const &operator[](size_t idx) const {
- if (idx >= this->_size)
- throw IndexOutOfBounds();
- return this->_elements[idx];
- };
- size_t size(void) const { return this->_size; };
- private:
- size_t _size;
- T *_elements;
- };
- #endif /* *********************************************************** ARRAY_H */
|