-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathC2DVector.h
58 lines (55 loc) · 1.32 KB
/
C2DVector.h
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
#ifndef C2DVECTOR_H_HEADER_INCLUDED_BECEF599
#define C2DVECTOR_H_HEADER_INCLUDED_BECEF599
#include <vector>
using namespace std;
template <class T>
class C2DVector
{
public:
C2DVector():m_dimRow(0), m_dimCol(0){;}
C2DVector(int nRow, int nCol) {
m_dimRow = nRow;
m_dimCol = nCol;
for (int i=0; i < nRow; i++){
vector<T> x(nCol);
int y = x.size();
m_2DVector.push_back(x);
}
}
void SetAt(int nRow, int nCol, const T& value) throw(std::out_of_range) {
if(nRow >= m_dimRow || nCol >= m_dimCol)
throw out_of_range("Array out of bound");
else
m_2DVector[nRow][nCol] = value;
}
T GetAt(int nRow, int nCol) {
if(nRow >= m_dimRow || nCol >= m_dimCol)
throw out_of_range("Array out of bound");
else
return m_2DVector[nRow][nCol];
}
void GrowRow(int newSize) {
if (newSize <= m_dimRow)
return;
m_dimRow = newSize;
for(int i = 0 ; i < newSize - m_dimCol; i++) {
vector<int> x(m_dimRow);
m_2DVector.push_back(x);
}
}
void GrowCol(int newSize) {
if(newSize <= m_dimCol)
return;
m_dimCol = newSize;
for (int i=0; i <m_dimRow; i++)
m_2DVector[i].resize(newSize);
}
vector<T>& operator[](int x) {
return m_2DVector[x];
}
private:
vector< vector <T> > m_2DVector;
unsigned int m_dimRow;
unsigned int m_dimCol;
};
#endif