Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
//
// 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;
}
}