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

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

1、先定义数组,后赋值

int arr[2][3];

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 arr[2][3] =

{

{1,2,3},

{4,5,6}

};

或者

int arr[2][3] =

{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;  
}

输出结果:

相关推荐
多恩Stone1 天前
【C++入门扫盲1】C++ 与 Python:类型、编译器/解释器与 CPU 的关系
开发语言·c++·人工智能·python·算法·3d·aigc
QQ4022054961 天前
Python+django+vue3预制菜半成品配菜平台
开发语言·python·django
遥遥江上月1 天前
Node.js + Stagehand + Python 部署
开发语言·python·node.js
蜡笔小马1 天前
21.Boost.Geometry disjoint、distance、envelope、equals、expand和for_each算法接口详解
c++·算法·boost
悠哉悠哉愿意1 天前
【单片机学习笔记】串口、超声波、NE555的同时使用
笔记·单片机·学习
m0_531237171 天前
C语言-数组练习进阶
c语言·开发语言·算法
超级大福宝1 天前
N皇后问题:经典回溯算法的一些分析
数据结构·c++·算法·leetcode
Railshiqian1 天前
给android源码下的模拟器添加两个后排屏的修改
android·开发语言·javascript
别催小唐敲代码1 天前
嵌入式学习路线
学习
雪人不是菜鸡1 天前
简单工厂模式
开发语言·算法·c#