http://www.math.ncu.edu.tw/~weihan/c++/lab/90_w14.html


又如
http://erdani.org/publications/compound_iterators.html#1





2. 指定 最 外层维的大小。
http://www.atomicmpc.com.au/forums.asp?s=2&c=10&t=3933
1
// create the vectors
2
vector<int> rows(10, 0); // vector of length 10 value 0
3
vector< vector<int> > columns(10, rows); // vector of length 10 holds rows
4
vector< vector < vector<int> > > data(1, columns); // vector of length 2 holds columns / rows
5
6
vector<int>::iterator myIterator;
7
vector<int>::const_iterator iter_r;
8
vector< vector<int> >::const_iterator iter_c;
9
vector< vector < vector<int> > >::const_iterator iter_d;
10

2

3

4

5

6

7

8

9

10

3.利用 temlate class 实现
http://www.codeguru.com/forum/showthread.php?t=231046
1
#include <vector>
2
3
template <typename T>
4
class dynamic_array
5
{
6
public:
7
dynamic_array(){};
8
dynamic_array(int rows, int cols)
9
{
10
for(int i=0; i<rows; ++i)
11
{
12
data_.push_back(std::vector<T>(cols));
13
}
14
}
15
16
// other ctors
.
17
18
inline std::vector<T> & operator[](int i) { return data_[i]; }
19
20
inline const std::vector<T> & operator[] (int i) const { return data_[i]; }
21
22
// other accessors, like at() 
23
24
void resize(int rows, int cols)
25
{
26
data_.resize(rows);
27
for(int i = 0; i < rows; ++i)
28
data_[i].resize(cols);
29
}
30
31
// other member functions, like reserve()
.
32
33
private:
34
std::vector<std::vector<T> > data_;
35
};
36
37
38
int main()
39
{
40
dynamic_array<int> a(3, 3);
41
a[1][1] = 2;
42
int x = a[1][1];
43
return 0;
44
}

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
