-
Notifications
You must be signed in to change notification settings - Fork 160
/
Iterator for matrix.cpp
62 lines (40 loc) · 1.01 KB
/
Iterator for matrix.cpp
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
/*
iterator for a matrix
*/
class MatrixIter {
public:
MatrixIter (vector<vector<int>>& matrix) : vecvec(matrix), ni(0), nj(0), Inited(false) {}
bool hasNext() {
// not last row
if (i < vecvec.size()-1) return true;
// last row, but not last column
if (i = vecvec.size()-1 && j < vecvec[i].size()-1) return true;
return false;
}
int getVal() { return vecvec[ni][nj]; }
void next() {
if (hasNext()) prob(ni, nj);
}
void first(){
if (vecvec.empty()) return;
ni = 0;
nj = 0;
}
private:
void prob(int& i, int& j) {//by reference
i = ni;
j = nj;
if (i < vecvec.size() && j < vecvec[i].size()-1) { //col++
j++;
return;
}
if (i < vecvec.size() && j = vecvec[i].size()-1) { //row++
i++;
j = 0;
return;
}
}
vector<vector<int>> &vecvec;
int ni;
int nj;
};