matrix(行列)をこしらえてみた(簡易版だけど)
C++
1#include <memory>
2
3template<typename T> class matrix {
4private:
5 std::size_t rows_;
6 std::size_t cols_;
7 std::unique_ptr<T[]> body_;
8public:
9 explicit matrix(std::size_t cols, std::size_t rows=1)
10 : rows_(rows), cols_(cols) {
11 body_ = std::make_unique<T[]>(cols_*rows_);
12 }
13 std::size_t rows() const { return rows_; }
14 std::size_t cols() const { return cols_; }
15 T* operator[](std::size_t row) { return body_.get()+row*cols_; }
16 const T* operator[](std::size_t row) const { return body_.get()+row*cols_; }
17};
18
19// おためし
20#include <iostream>
21
22int main(){
23 int H,W;
24 std::cin >> H >> W;
25 matrix<int> ans(H, W);
26
27 for (int i = 0; i < 3; i++) {
28 for(int j = 0; j < 5;j++){
29 int num;
30 std::cin >> num;
31 ans[i][j] = num;
32 }
33 }
34
35 for ( int i = 0; i < ans.rows(); ++i ) {
36 for ( int j = 0; j < ans.cols(); ++j ) {
37 std::cout << ans[i][j] << ' ';
38 }
39 std::cout << std::endl;
40 }
41}
※ ただしこのmatirx、unique_ptr使っているのでコピーできません。