multidimensional vector array
1. 不指定维的大小
http://www.math.ncu.edu.tw/~weihan/c++/lab/90_w14.html
typedef vector< int > array1d ;
typedef vector< array1d > array2d ;
又如
http://erdani.org/publications/compound_iterators.html#1
typedef std::vector<something> row;
typedef std::vector<row> matrix;
typedef std::vector<matrix> cube;
![]()
2. 指定 最 外层维的大小。
http://www.atomicmpc.com.au/forums.asp?s=2&c=10&t=3933
3.利用 temlate class 实现
http://www.codeguru.com/forum/showthread.php?t=231046
http://www.math.ncu.edu.tw/~weihan/c++/lab/90_w14.html
typedef vector< int > array1d ;
typedef vector< array1d > array2d ;又如
http://erdani.org/publications/compound_iterators.html#1
typedef std::vector<something> row;
typedef std::vector<row> matrix;
typedef std::vector<matrix> cube;

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![]()
// 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

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
}
#include <vector>2

3
template <typename T>4
class dynamic_array5
{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
}

dynamic_array()
浙公网安备 33010602011771号