动态创建二维数组

vector创建二维数组
初始化二维数组
vector<vector <int> > ivec(n ,vector<int>(m));      //n*m的二维vector
vector<vector <int> > ivec(n , vector<int>(m, 0));  //n*m的二维vector,所有元素为0

动态创建m*n的二维
方法一:
vector<vector <int> > ivec;
ivec.resize(n);
for(int i = 0; i < n; i++) ivec[i].resize(m);

方法二:
vector<vector <int> > ivec;
ivec.resize(n, vector<int>(m));


动态创建二维数组a[n][m]
C语言版:
#include<malloc.h>
int **a = (int **)malloc(n *sizeof(int *));
for(int i = 0; i < n; i++)
    a[i] = (int *)malloc(m *sizeof(int));

C++版:
int **a = new int *[n];
for(int i = 0; i < n; i++) a[i] = new int[m];

  

posted @ 2017-05-03 18:31  zxzhang  阅读(784)  评论(0编辑  收藏  举报