weidagang2046的專欄

          物格而后知致
          隨筆 - 8, 文章 - 409, 評論 - 101, 引用 - 0

          導航

          <2025年7月>
          293012345
          6789101112
          13141516171819
          20212223242526
          272829303112
          3456789

          常用鏈接

          留言簿(12)

          隨筆檔案(8)

          文章分類(421)

          文章檔案(409)

          相冊

          Link

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          C++中動態分配二維數組的方法

          ?

          #include

          #define ?N?4
          typedef?
          int ?p[N]; // 方法一用到

          using ? namespace ?std;

          int ?main()
          {
          ????
          int ?n? = ? 0 ;

          ????
          // 方法一:使用typedef定義一個具有N個元素的數組類型
          ????p? * ptr1;?????? // 定義二維數組??用法與二維數組相同
          ????ptr1? = ? new ?p[N];

          ????
          for ( int ?i? = ? 0 ;?i? < ?N;?i ++ )
          ????????
          for ( int ?j? = ? 0 ;?j? < ?N;?j ++ )
          ????????????ptr1[i][j]?
          = ? ++ n;

          ????cout?
          << ? " 方法一: " ? << ?endl;
          ????
          for (i? = ? 0 ;?i? < ?N;?i ++ )
          ????{
          ????????
          for ( int ?j = 0 ;j? < ?N;?j ++ )
          ????????????cout?
          << ?ptr1[i][j]? << ? " ? " ;
          ????????cout?
          << ?endl;
          ????}
          ????delete[]?ptr1;
          ????cout?
          << ?endl;

          ????
          // ?方法二:使用數組指針
          ???? int ?row? = ?N;????? // 二維數組的行數?
          ???? int ?column? = ?N;?? // 二維數組的列數
          ?????
          ????
          // 分配一個指針數組,其首地址保存在pMatrix中
          ???? int ? ** pMatrix? = ? new ? int * [row];

          ????
          // 為指針數組的每個元素分配一個數組
          ???? for ?( int ?i? = ? 0 ;?i? < ?row;?i ++ )
          ????????pMatrix[i]?
          = ? new ? int [column];

          ????
          // 以上是分配,以下是釋放
          ???? for ?( int ?i? = ? 0 ;?i? < ?row;?i ++ )
          ????????delete?[column]?pMatrix[i];
          ????delete?[row]?pMatrix;

          ????
          // 這些技術可用于構造一個矩陣類

          ????
          return ? 0 ;
          }
          from: http://www.zahui.com/html/9/20062.htm

          posted on 2006-05-16 21:39 weidagang2046 閱讀(858) 評論(0)  編輯  收藏 所屬分類: C/C++

          主站蜘蛛池模板: 三江| 郸城县| 雅安市| 广元市| 普兰县| 鄢陵县| 晋宁县| 五寨县| 岫岩| 保山市| 四平市| 永泰县| 临沂市| 井研县| 什邡市| 宁河县| 扶风县| 栾川县| 闽侯县| 马公市| 金阳县| 梅州市| 慈溪市| 延安市| 大新县| 石台县| 孟连| 聊城市| 襄垣县| 绥宁县| 西盟| 黄山市| 江达县| 杭锦后旗| 道孚县| 个旧市| 丘北县| 赫章县| 台南市| 策勒县| 民丰县|