C++實現動態數組

來源:互聯網
上載者:User

一、雙指標方法

雙指標就是指像指標的指標,比如你可以這樣聲明一個數組和釋放一個數組的空間:   
  //雙指標聲明二維動態數組<br /> int **arr = new int*[x]; //理解雙指標的含義,指標的指標->數組的指標->二維數組<br /> for(int i=0; i < x; ++i)<br /> arr[i] = new int[y]; //可以理解為在一個一維數組的某一個位上再開闢一個數組,也就形成二維數組</p><p>//用delete釋放空間<br />for(int m=0;m < x; m++)<br /> delete[] p[m];<br /> delete[] p;<br />

二、STL vector

聲明方法如下:

//用vector 聲明一個m*n的數組<br />vector<vector<int> > vecArr(m,vector<int>(n)); //注意 > >之間要有空格,不然會誤判為>>操作符<br /> for(int i = 0;i < m;i++)<br /> for(j = 0;j < n;j++)<br /> vecArr[i][j] = i*j; </p><p>//聲明一個i*j*k的三維數組<br />vector<vector<vector<int> > > vecAnt(m,vector<vector<int> >(n, vector<int>(l)));<br /> for(int i = 0;i < m;i++)<br /> for(int j = 0;j < n;j++)<br /> for(k = 0;k < l;k++)<br /> vecInt[i][j][k] = i+j+k;


 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.