Skip to content

Instantly share code, notes, and snippets.

@njlr
Created June 2, 2017 17:30
Show Gist options
  • Save njlr/5714c5bd4cf5f9313981f40f5a99697d to your computer and use it in GitHub Desktop.
Save njlr/5714c5bd4cf5f9313981f40f5a99697d to your computer and use it in GitHub Desktop.
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