Created
November 12, 2012 03:40
-
-
Save hindol/4057378 to your computer and use it in GitHub Desktop.
2D Array Class
This file contains 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
#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