首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

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

2013-03-25 
用STL中的vector动态开辟二维数组用STL中的vector动态开辟二维数组源代码:#include iostream#include v

用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;
}




热点排行