C++入门学习(三十二)二维数组定义方式

一维数组类似于一条"线",而二维数组类似于一个"面",二维数组也更像一个表格,由我们在"表格"中查询数据。

1、先定义数组,后赋值

int arr23;

cpp 复制代码
#include <iostream>  
using namespace std;
 
  
int main() {  
    int arr[2][3];
    arr[0][0] = 1;
    arr[0][1] = 1;
    arr[0][2] = 1;

    arr[1][0] = 1;
    arr[1][1] = 1;
    arr[1][2] = 1;
		 
   cout<<arr[0][2]<<endl;

  
    return 0;  
}

2、定义二维数组的时候,一并赋值

int arr23 =

{

{1,2,3},

{4,5,6}

};

或者

int arr23 =

{1,2,3,4,5,6};

cpp 复制代码
#include <iostream>  
using namespace std;
 
  
int main() {  
   
    int arr[2][3] =
    {
    	{1,2,3},
    	{4,5,6}
		
	};
    for(int i=0;i<2;i++)
	{
        for(int j=0;j<3;j++)
    {
    	cout<<arr[i][j]<<" "<<endl;
	}
 }

  
    return 0;  
}

3、可以省略行数照样可以定义数组

int arr\[\]3 =

{1,2,3,4,5,6,7,8,9};

cpp 复制代码
#include <iostream>  
using namespace std;
 
  
int main() {  
   
    int arr[][3] =
    {1,2,3,4,5,6,7,8,9};
    
    for(int i=0;i<3;i++)
	{
        for(int j=0;j<3;j++)
    {
    	cout<<arr[i][j]<<" "<<endl;
	}
 }

  
    return 0;  
}

输出结果:

相关推荐
一尘之中7 小时前
从C语言底层设计到系统架构评估:软件架构知识体系全景
学习·系统架构·ai写作
NiceCloud喜云7 小时前
Opus 4.8 的 Effort Control 怎么选:Low 到 Max 五档策略
android·java·大数据·前端·c++·python·spring
cjhbachelor7 小时前
c++继承
c++
AI玫瑰助手8 小时前
Python函数:默认参数的定义与注意事项
开发语言·python·信息可视化
油炸自行车8 小时前
Claude Code 错误:API Error: 400 Failed to deserialize the JSON body into the
开发语言·javascript·json·trae·claude code·api error 400
肩上风骋8 小时前
C++14特性
开发语言·c++·c++14特性
星夜夏空998 小时前
FreeRTOS学习(4)——内存映射
数据库·学习·mongodb
不羁的木木9 小时前
ArkWeb实战学习笔记05-综合实战:构建混合应用
笔记·学习·harmonyos
橙橙笔记9 小时前
Python的学习第一部分
python·学习
bush49 小时前
嵌入式linux学习记录二
linux·运维·学习