Skip to content

Instantly share code, notes, and snippets.

@hindol
Created November 12, 2012 03:40
Show Gist options
  • Save hindol/4057378 to your computer and use it in GitHub Desktop.
Save hindol/4057378 to your computer and use it in GitHub Desktop.
2D Array Class
#include <cstddef>
#include <vector>
template <typename T>
class Array2D
{
public:
Array2D()
: height_(0), width_(0)
{}
Array2D(size_t height, size_t width, const T& init_with = T())
: height_(height), width_(width),
vec_(height_ * width_, init_with)
{}
void Resize(size_t height, size_t width, const T& init_with = T())
{
height_ = height;
width_ = width;
vec_.resize(height_ * width_, init_with);
}
const T& At(size_t row, size_t col) const
{ return vec_[row * width_ + col]; }
T& At(size_t row, size_t col)
{ return vec_[row * width_ + col]; }
private:
size_t height_;
size_t width_;
std::vector<T> vec_;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment