转:用STL中的vector动态开辟二维数组

用STL中的vector动态开辟二维数组


源代码:
#include <iostream>
#include <vector>
using namespace std;
int main()
{
 int m, //行数
     n; //列数
 cout << "input value for m,n:";
 cin>>m>>n;
 
 //注意下面这一行:vector<int后两个">"之间要有空格!否则会被认为是重载">>"。
 vector<vector<int> > vecInt(m, vector<int>(n)); 
 for (int i = 0; i < m; i++)    //初始化二维数组,,其实这里可以不用初始化的,vector中默认初始化为0
   for (int j = 0; j < n; j++)
      vecInt[i][j] = 0;
  
 for (i = 0; i < m; i++)         //输出二维数组vecInt[][]
 {
  for (j = 0; j < n; j++)
   cout<<vecInt[i][j]<<" ";
  cout<<endl;
 }
 return 0;
}

原文地址:https://www.cnblogs.com/acbingo/p/4556466.html