• 动态多维数组在 VC 中的应用 2


    怎样给多维数组动态分配内存

    //Allocate:
    int **p = new int* [m];
    for(int i = 0 ; i < m ; i++)
      p[i] = new int[n];

    //Use:
    for(int i = 0 ; i < m; i++)
      for(int j = 0 ; j < n ; j++)
        p[i][j] = i * j;

    //Free:
    for(int i = 0 ; i < m ; i++)
      delete[] p[i];
    delete[] p;


    1. 演示形为int[2][3]的二维动态数组
    ///////////////////////////////////////////////////////////////////
    int n1, n2;
    const int DIM1 = 2;
    const int DIM2 = 3;
    // 构造数组
    int **ppi = new int*[DIM1];
    for(n1 = 0; n1 < DIM1; n1++)
    {
      ppi[n1] = new int[DIM2];
    }
    // 填充数据
    for(n1 = 0; n1 < DIM1; n1++)
    {
      for(n2 = 0; n2 < DIM2; n2++)
      {
        ppi[n1][n2] = n1 * 10 + n2;
      }
    }
    // 输出
    for(n1 = 0; n1 < DIM1; n1++)
    {
      for(n2 = 0; n2 < DIM2; n2++)
      {
        afxDump << "ppi[" << n1 << "][" << n2 << "] = " << ppi[n1][n2] << "\n";
      }
    }
    // 释放数组
    for(n1 = 0; n1 < DIM1; n1++)
    {
      delete [] ppi[n1];
    }
    delete [] ppi;

    2. 三维动态数组(int[2][3][4])
    ///////////////////////////////////////////////////////////////////
    int n1, n2, n3;
    const int DIM1 = 2;
    const int DIM2 = 3;
    const int DIM3 = 4;
    // 构造数组
    int ***ppi = new int**[DIM1];
    for(n1 = 0; n1 < DIM1; n1++)
    {
      ppi[n1] = new int*[DIM2];
      for(n2 = 0; n2 < DIM2; n2++)
      {
        ppi[n1][n2] = new int[DIM3];
      }
    }
    // 填充数据
    for(n1 = 0; n1 < DIM1; n1++)
    {
      for(n2 = 0; n2 < DIM2; n2++)
      {
        for(n3 = 0; n3 < DIM3; n3++)
        {
          ppi[n1][n2][n3] = n1 * 100 + n2 * 10 + n3;
        }
      }
    }
    // 输出
    for(n1 = 0; n1 < DIM1; n1++)
    {
      for(n2 = 0; n2 < DIM2; n2++)
      {
        for(n3 = 0; n3 < DIM3; n3++)
        {
          afxDump << "ppi[" << n1 << "][" << n2 << "][" << n3 << "] = "<< ppi[n1][n2][n3] << "\n";
        }
      }
    }
    // 释放数组
    for(n1 = 0; n1 < DIM1; n1++)
    {
      for(n2 = 0; n2 < DIM2; n2++)
      {
        delete [] ppi[n1][n2];
      }
      delete [] ppi[n1];
    }
    delete [] ppi;
     
    转自:http://blog.gisforum.net/u/28376/archives/2007/316.html
  • 相关阅读:
    浏览器缓存
    HTML学习笔记(九) Web Socket
    [转]maven2中snapshot快照库和release发布库的区别和作用
    [转]maven2中snapshot快照库和release发布库的应用
    CentOS 7 mini 试用笔记
    Windows找出占用端口的进程
    ORA-24408: could not generate unique server group name
    Oracle中NVL、NVL2、NULLIF 三个函数的区别?
    【代码大全-读书笔记】第1章 欢迎进入软件构建的世界
    【代码大全-读书笔记】前言
  • 原文地址:https://www.cnblogs.com/sikale/p/1967530.html
Copyright © 2020-2023  润新知