zoukankan      html  css  js  c++  java
  • C++中创建动态2维数组

     1 #include<iostream>
     2 #include<iomanip>
     3 using namespace std;
     4 
     5 template<class Type>
     6 void Make2DArray(Type **&x,int rows,int cols)
     7 {
     8     // 创建一个行指针
     9     x=new Type *[rows];
    10     for(int i=0;i<rows;i++)
    11     {
    12         x[i]=new Type [cols];
    13     }
    14 
    15 }
    16 
    17 int main()
    18 {
    19     int rows,cols,i,j;
    20     float **x;
    21 
    22     cout<<"输入创建动态数组的行数rows= ";
    23     cin>>rows;
    24     cout<<"输入创建动态数组的列数cols= ";
    25     cin>>cols;
    26     Make2DArray(x,rows,cols);  // 声明一个2维数组,这条语句要放在下面的for循环之前
    27     for(i=0;i<rows;i++)
    28     {
    29         for(j=0;j<cols;j++)
    30         {
    31              x[i][j]=j+i*cols;
    32         }
    33     }
    34     
    35     cout<<"输出创建的数组:
    ";
    36     for(i=0;i<rows;i++)
    37     {
    38         for(j=0;j<cols;j++)
    39         {
    40              cout<<setw(3)<<x[i][j];
    41         }
    42         cout<<endl;
    43     }
    44        return 0;
    45 }

    注意:Make2DArray(x,rows,cols); 是为了声明一个rows行,cols列的2维数组;

  • 相关阅读:
    Java EE企业应用发展
    黄金点游戏
    C++ Word Count 发布程序
    C++原创应用类库和工具类库
    软件之魂
    latex表格multirow的使用
    web service和ejb的区别
    RPC
    hashcode()和equals()方法
    JSON
  • 原文地址:https://www.cnblogs.com/yuzhuwei/p/4173641.html
Copyright © 2011-2022 走看看