-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatrix.cpp
More file actions
70 lines (55 loc) · 1.32 KB
/
matrix.cpp
File metadata and controls
70 lines (55 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <vector>
#include <iostream>
struct IndexedElement {
int element;
size_t index;
};
template<class T>
class Matrix;
template<class T>
class MatrixLeftProxy {
public:
MatrixLeftProxy(Matrix<T>* matrix, size_t column)
: matrix_(matrix), column_(column)
{}
T& operator > (size_t row) {
return matrix_->table_.at(column_).at(row);
}
const T& operator > (size_t row) const {
return matrix_->table_.at(column_).at(row);
}
private:
Matrix<T>* matrix_;
size_t column_;
};
template<class T>
class Matrix {
typedef MatrixLeftProxy<T> LeftProxy;
public:
Matrix(size_t nColumns, size_t nRows)
: table_(nColumns, std::vector<T>(nRows))
{}
LeftProxy operator < (size_t column) {
return LeftProxy(this, column);
}
private:
friend class LeftProxy;
std::vector< std::vector<T> > table_;
};
int main_()
{
Matrix<int> matrix(3, 4);
matrix<1>2 = 4;
std::cout << (matrix<1>1) << " " << (matrix<1>2) << std::endl;
return 0;
std::vector<int> a(5, 2);
for (size_t i = 0; i < 7; ++i) {
try {
std::cout << a.at(i) << std::endl;
} catch (std::exception& ex) {
std::cerr << "Sorry, no index " << i << "\n";
std::cerr << ex.what() << "\n";
}
}
return 0;
}