c/c++教程 - 1.7 数组 一维数组 二维数组

九、数组

数组:一个里面存放了相同类型数据元素的集合。

特点:每个数组中的元素都是相同的数据类型;数组是由连续的内存位置组成的。

注意:数组的下标是从0开始索引。

参考视频https://www.bilibili.com/video/BV1et411b73Z?from=search&seid=4205594350351753444

已投币三连,非常好的视频教程,感谢up主的奉献。

 

(1.1)一维数组

一维数组的三种定义方式:

数据类型 数组名[ 数组长度 ];
数据类型 数组名[ 数组长度 ] = { 值1, 值2 ...};
数据类型 数组名[ ] = { 值1, 值2 ...};

int str1[5];
int str1[5] = { -2, -1, 0, 1, 2};
int str1[] = { -2, -1, 0, 1, 2};
	int i=0;
	int str1[5];
	str1[0] = -2;
	str1[1] = -1;
	str1[2] = 0;
	str1[3] = 1;
	str1[4] = 2;

	for (i = 0; i <5; i++)
	{
		cout << str1[i] << " ";
	}
	cout << endl;
----------------------------------------------------------
-2 -1 0 1 2
请按任意键继续. . .
	int i=0;
	int str1[5] = { -2, -1, 0, 1, 2 };

	for (i = 0; i <5; i++)
	{
		cout << str1[i] << " ";
	}
	cout << endl;
-----------------------------------------------------------
-2 -1 0 1 2
请按任意键继续. . .
	int i=0;
	int str1[] = { -2, -1, 0, 1, 2 };

	for (i = 0; i <5; i++)
	{
		cout << str1[i] << " ";
	}
	cout << endl;
-------------------------------------------------------
-2 -1 0 1 2
请按任意键继续. . .

 

(1.2)一维数组数组名

一维数组名称的用途:

1.可以统计整个数组在内存中的长度。

2.可以获取数组在内存中的首地址。

	int i=0;
	int str1[5] = { -2, -1, 0, 1, 2 };

	for (i = 0; i <5; i++)
	{
		cout << str1[i] << " ";
	}
	cout << endl;

	cout << "str1[]整个数组占用的空间:" << sizeof(str1) << endl;
	cout << "str1[0]元素占用的空间:" << sizeof(str1[0]) << endl;
	cout << "str1[]数组中元素个数:" << sizeof(str1) / sizeof(str1[0]) << endl;
	cout << "str1[]数组的内存首地址:" << str1 << endl;
	cout << "str1[0]元素的内存首地址:" << &str1[0] << endl;
	cout << "str1[1]元素的内存首地址:" << &str1[1] << endl;
----------------------------------------------------------------------------------------
-2 -1 0 1 2
str1[]整个数组占用的空间:20
str1[0]元素占用的空间:4
str1[]数组中元素个数:5
str1[]数组的内存首地址:00D8FA64
str1[0]元素的内存首地址:00D8FA64
str1[1]元素的内存首地址:00D8FA68
请按任意键继续. . .

 

(1.3)一维数组常用算法

寻找最大值:

	int i=0;
	int str1[5] = { -2, -1, 0, 1, 2 };
	int start = 0;
	int end = sizeof(str1) / sizeof(str1[0]);
	int xtemp = 0;

	for (i = 0; i < end; i++)
	{
		if (str1[i] > str1[xtemp])
		{
			xtemp = i;
		}
	}

	for (i = 0; i < 5; i++)
	{
		cout << str1[i] << " ";
	}
	cout << endl;
	cout << "str1[]数组中元素个数:" << end << endl;
	cout << "str1[]数组中第几个元素为最大值:" << xtemp + 1 << endl;
	cout << "最大值为:" << str1[xtemp] << endl;
--------------------------------------------------------------------------
-2 -1 0 1 2
str1[]数组中元素个数:5
str1[]数组中第几个元素为最大值:5
最大值为:2
请按任意键继续. . .

 

逆置(整个数组首位互换):

	int str1[5] = { -2, -1, 0, 1, 2 };
	int start = 0;
	int end = sizeof(str1) / sizeof(str1[0]) - 1;
	int xtemp = 0;

	for (i = 0; i < 5; i++)
	{
		cout << str1[i] << " ";
	}
	cout << endl;

	while (start < end)
	{
		xtemp = str1[start];
		str1[start] = str1[end];
		str1[end] = xtemp;
		start++;
		end--;
	}

	for (i = 0; i < 5; i++)
	{
		cout << str1[i] << " ";
	}
	cout << endl;
--------------------------------------------------------------
-2 -1 0 1 2
2 1 0 -1 -2
请按任意键继续. . .

 

冒泡排序:

	int i=0,j=0;
	int str1[5] = { 8, 2, 5, 4, 6 };
	int start = 0;
	int end = sizeof(str1) / sizeof(str1[0]) - 1;
	int xtemp = 0;

	cout << "数组原始值:";
	for (i = 0; i < 5; i++)
	{
		cout << str1[i] << " ";
	}
	cout << endl;

	for (i = end; i > 0; i--)
	{
		for (j = 0; j < i; j++)
		{
			if (str1[j] > str1[j + 1])
			{
				xtemp = str1[j];
				str1[j] = str1[j + 1];
				str1[j + 1] = xtemp;
			}
		}
	}

	cout << "冒泡排序后:";
	for (i = 0; i < 5; i++)
	{
		cout << str1[i] << " ";
	}
	cout << endl;
----------------------------------------------------------------------
数组原始值:8 2 5 4 6
冒泡排序后:2 4 5 6 8
请按任意键继续. . .

 

(2.1)二维数组

二维数组的四种定义方式:

数据类型 数组名[ 行数 ][ 列数 ];
数据类型 数组名[ 行数 ][ 列数 ] = { { 值1, 值2 ...}, { 值11, 值12 ...},  ... };
数据类型 数组名[ 行数 ][ 列数 ] = { 值1, 值2, 值3, 值4 ...};
数据类型 数组名[  ][ 列数 ] = { 值1, 值2, 值3, 值4 ...};

int str1[2][3];
int str1[2][3] = { { -2, -1, 0}, { 1, 2, 3} };
int str1[2][3] = { -2, -1, 0, 1, 2, 3};
int str1[][3] = { -2, -1, 0, 1, 2, 3};
	int i=0,j=0;
	int str1[2][3] = { { -2, -1, 0}, { 1, 2, 3} };
	//int str1[2][3] = { -2, -1, 0, 1, 2, 3 };
	//int str1[][3] = { -2, -1, 0, 1, 2, 3 };

	for (i = 0; i < 2; i++)
	{
		for (j = 0; j < 3; j++)
		{
			cout << str1[i][j] << " ";
		}
		cout << endl;
	}
----------------------------------------------------------------
-2 -1 0
1 2 3
请按任意键继续. . .

 

(2.2)二维数组数组名

二维数组名称的用途:

1.统计二维数组所占内存空间。

2.获取二维数组在内存中的首地址。

	int i = 0,j = 0;
	int str1[2][3] = 
	{ 
		{ -2, -1, 0},
		{ 1, 2, 3}
	};

	for (i = 0; i < 2; i++)
	{
		for (j = 0; j < 3; j++)
		{
			cout << str1[i][j] << " ";
		}
		cout << endl;
	}

	cout << "str1[][]二维数组占用的空间:" << sizeof(str1) << endl;
	cout << "str1[][]第一行占用的空间:" << sizeof(str1[0]) << endl;
	cout << "str1[0][0]元素占用的空间:" << sizeof(str1[0][0]) << endl;
	cout << endl;
	cout << "str1[][]二维数组的行数:" << sizeof(str1) / sizeof(str1[0]) << endl;
	cout << "str1[][]二维数组的列数:" << sizeof(str1[0]) / sizeof(str1[0][0]) << endl;
	cout << "str1[][]二维数组中所有元素个数:" << sizeof(str1) / sizeof(str1[0][0]) << endl;
	cout << endl;
	cout << "str1[][]二维数组的内存首地址:" << str1 << endl;
	cout << "str1[0][0]第一行行首的内存首地址:" << &str1[0][0] << endl;
	cout << "str1[0][1]元素的内存首地址:" << &str1[0][1] << endl;
	cout << "str1[1][0]第二行行首的内存首地址:" << &str1[1][0] << endl;
-----------------------------------------------------------------------------------------
-2 -1 0
1 2 3
str1[][]二维数组占用的空间:24
str1[][]第一行占用的空间:12
str1[0][0]元素占用的空间:4

str1[][]二维数组的行数:2
str1[][]二维数组的列数:3
str1[][]二维数组中所有元素个数:6

str1[][]二维数组的内存首地址:00CFF844
str1[0][0]第一行行首的内存首地址:00CFF844
str1[0][1]元素的内存首地址:00CFF848
str1[1][0]第二行行首的内存首地址:00CFF850
请按任意键继续. . .

(2.3)二维数组应用案例

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值