Skip to content

Instantly share code, notes, and snippets.

@jfalcou
Created August 2, 2021 08:18
Show Gist options
  • Save jfalcou/2abc853d31fe320eb3bc7365c096b2cf to your computer and use it in GitHub Desktop.
Save jfalcou/2abc853d31fe320eb3bc7365c096b2cf to your computer and use it in GitHub Desktop.
#pragma once
#include <vector>
#include <ostream>
template<typename T> struct matrix
{
using value_type = T;
public:
matrix(std::size_t w = 0, std::size_t h = 0)
: data_(h*w),height_(h),width_(w) {}
value_type& operator()(std::size_t col, std::size_t row)
{
return data_[col + row*width_];
}
value_type operator()(std::size_t col, std::size_t row) const
{
return data_[col + row*width_];
}
std::size_t width() const { return width_; }
std::size_t height() const { return height_; }
private:
std::vector<value_type> data_;
std::size_t height_, width_;
};
std::ostream& operator<<(std::ostream& os, matrix const& mat)
{
for(std::size_t i=0;i!=mat.height();++i)
{
for(std::size_t j=0;j!=mat.width();++j)
std::cout << mat(j,i) << " ";
std::cout << "\n";
}
return os;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment