zoukankan      html  css  js  c++  java
  • C++多维数组

    一维数组

    int ia[] = {0,1,2,3,4,5,6,7,8,9}; //ia是数组

    多维数组

    C++ 中并没有多维数组,它就是数组的数组。

    int ia[3][4];
    

    初始化

    int ia[3][4] = {
        {0, 1, 2, 3},
        {4, 5, 6, 7},
        {8, 9, 10, 11}
    };
    //等价于
    int ia[3][4] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
    //显示初始化每行的首元素
    int ia[3][4] = { {0} , {4} , {8}};
    //显示初始化第一行
    int ix[3][4] = {0, 3, 6, 9};
    

    引用

    下标引用

    通过ia[i][j]方式引用下标:

    constexpr size_t rowCnt = 3, colCnt = 4;
    int ia[rowCnt][colCnt];
    
    for (size_t i = 0; i != rowCnt; i++) {
      for (size_t j = 0; j != colCnt; j++) {
        ia[i][j] = i * colCnt + j;
      }
    }
    

    指针引用

    Point root_points[2][4];
    root_points[0][0] = Point(215,220);
    root_points[0][1] = Point(460,225);
    root_points[0][2] = Point(466,450);
    root_points[0][3] = Point(235,465);
    
    root_points[1][0] = Point(800,800);
    root_points[1][1] = Point(800,800);
    root_points[1][2] = Point(800,800);
    root_points[1][3] = Point(800,800);
    
    //Point* ppt[1] = {root_points[0]}; //此时ppt[1]是第一行数据的首地址,为215,220
      Point* ppt[1] = {root_points[1]}; //此时ppt[1]是第二行数据的首地址,为800,800
    

    参考链接:
    https://zhuanlan.zhihu.com/p/65862735

  • 相关阅读:
    CCF NOI1079 合法C标识符
    CCF NOI1080 统计字符
    CCF NOI1076 进制转换
    CCF NOI1065 最小公倍数
    CCF NOI1139 高精度减法
    CCF NOI1138 高精度加法
    CCF NOI1115 找数
    CCF NOI1097 数列
    CCF NOI1089 高精度运算
    NUC1931 Problem D 区间素数【素数筛选】
  • 原文地址:https://www.cnblogs.com/chendeqiang/p/13207750.html
Copyright © 2011-2022 走看看