Created
June 2, 2017 17:30
-
-
Save njlr/5714c5bd4cf5f9313981f40f5a99697d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
template<int D> | |
struct Vector { | |
static constexpr unsigned N = D; | |
int data[N]; | |
Vector(Vector<D> const& v) { | |
for (int i = 0; i < N; ++i) { | |
data[i] = v[i]; | |
} | |
} | |
Vector(int fill = 0) { | |
for (int i = 0; i < N; ++i) { | |
data[i] = fill; | |
} | |
} | |
int sum() const { | |
int result = 0; | |
for (int i = 0; i < N; ++i) { | |
result += data[i]; | |
} | |
return result; | |
} | |
int* begin() { return &data[0]; } | |
int* end() { return &data[N]; } | |
int const* begin() const { return &data[0]; } | |
int const* end() const { return &data[N]; } | |
int& operator[] (unsigned const& i) { | |
if (N <= i) throw "out of bound"; | |
return data[i]; | |
} | |
int const& operator[] (unsigned const& i) const { | |
if (N <= i) throw "out of bound"; | |
return data[i]; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment