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
 1// create the vectors 
 2vector<int> rows(100); // vector of length 10 value 0 
 3vector< vector<int> > columns(10, rows); // vector of length 10 holds rows 
 4vector< vector < vector<int> > > data(1, columns); // vector of length 2 holds columns / rows 
 5
 6vector<int>::iterator myIterator; 
 7vector<int>::const_iterator iter_r; 
 8vector< vector<int> >::const_iterator iter_c; 
 9vector< vector < vector<int> > >::const_iterator iter_d; 
10


3.利用 temlate class 实现
http://www.codeguru.com/forum/showthread.php?t=231046
 1#include <vector>
 2
 3template <typename T>
 4class dynamic_array
 5{
 6public:
 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
33private:
34  std::vector<std::vector<T> > data_;  
35}
;
36
37
38int main()
39{
40  dynamic_array<int> a(33);
41  a[1][1= 2;
42  int x = a[1][1];
43  return 0;
44}







posted on 2006-12-04 22:50  cy163  阅读(381)  评论(0编辑  收藏  举报

导航