zoukankan      html  css  js  c++  java
  • 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}







  • 相关阅读:
    ES6、ES7、ES8特性
    【react】XXX项目环境搭建
    map
    vector
    list
    米勒素数模板
    POJ-2421-Constructing Roads(最小生成树 普利姆)
    HDU1301 Jungle Roads(Kruskal)
    Truck History(prime)
    phpstorm快捷键和激活
  • 原文地址:https://www.cnblogs.com/cy163/p/582070.html
Copyright © 2011-2022 走看看