006_C++数组

数组与数组下标

数组

在C++中要声明一个数组,需要指定元素的类型和元素数量,如下所示:

type arrayname[arraysize];

这叫做一维数组,arraysize必须是一个大于零的整数常量,type可以是任意有效的数据类型,arrayname是数组名(遵循标识符的命名规则)。

例如,要声明一个类型为int的包含10个元素的数组numbers,声明语句如下:

int numbers[10];

现在numbers是一个可用的数组,可以容纳10个int类型的数字。

#include <iostream> 
using namespace std; 

int main() { 
	// 定义了一个一维数组,numbers可以存储10个int元素 
	int numbers[10];

    return 0; 
}
下标
int array[6] = {1, 2, 3, 4, 5, 6};

对于数组array,数组中的首元素可以由array[0]访问,[ ]中的0就是array数组元素的下标之一,数组元素下标索引从0开始。所以这里我们最多能访问到array[5],当访问到array[6]时会发生数组溢出导致报错,所以对于长度为n的数组,我们最多访问到下标为n-1的元素。一定要注意,防止访问越界!

#include <iostream> 
using namespace std; 

int main() { 
	// 定义了一个一维数组,numbers可以存储10个int元素 
	//int numbers[10];
	int arr[6] = {1,2,3,4,5,6}; // 数组定义时,[]内为常量
	// 下标       0 1 2 3 4 5 
	// 访问数组中下标0对应的元素,[]内可以是常量,也可以是变量
	cout << arr[0] << endl; 

    return 0; 
}

数组的读入与输出

一维数组的输入与输出

通过下标访问数组,用for循环批量顺序访问

#include <iostream> 
using namespace std; 

int a[110]; // 全局数组,默认初始化数组中所有元素为0 
int main() { 
	int n; cin >> n;
	for(int i = 0; i < n; i++){
		// 写入全局数组 
		cin >> a[i];
	} 
	for(int i = 0; i < n; i++){
		cout << a[i] << " ";
	}

    return 0; 
}

运行结果:

5
2 4 5 6 9
2 4 5 6 9

数组的下标虽然从0开始,然而我们更习惯1代表第一个数,2代表第二个数,所以我们在使用数组时,往往下标0位置空着不用,真正的有效数据从下标1开始,第n个元素下标为n

#include <iostream> 
using namespace std; 

int a[110]; // 全局数组,默认初始化数组中所有元素为0 
int main() { 
	int n; cin >> n;
	for(int i = 1; i <= n; i++){
		// 写入全局数组 
		cin >> a[i];
	} 
	for(int i = 1; i <= n; i++){
		cout << a[i] << " ";
	}

    return 0; 
}

实战演练:数组倒序

#include <iostream> 
using namespace std; 

int n, a[110];  
int main() { 
	cin >> n;
	for(int i = 1; i <= n; i++) cin >> a[i];
	for(int i = n; i >= 1; i--){
		cout << a[i] << " ";
	}

    return 0; 
}

运行结果:

5
1 2 3 4 5
5 4 3 2 1

数组的基本操作

数组的查询和修改

输入n个数,存入数组,找到其中元素x,并修改为y,打印数组

#include <iostream> 
using namespace std; 

int n, a[110];  
int main() { 
	cin >> n;
	for(int i = 1; i <= n; i++) cin >> a[i];
	int x, y; cin >> x >> y;
	for(int i = 1; i <= n; i++){
		if(a[i] == x){  // 查询 
			a[i] = y;  // 修改 
		}
	}
	for(int i = 1; i <= n; i++) cout << a[i] << " ";

    return 0; 
}

运行结果:

5
2 4 6 6 8
6 10
2 4 10 10 8
数组的插入

输入n个数,存入数组,在给定位置插入新元素x,打印数组

#include <iostream> 
using namespace std; 

int n, a[110];  
int main() { 
	cin >> n;
	for(int i = 1; i <= n; i++) cin >> a[i];
	
	int x, y; cin >> x >> y;
	// 1. 从x位置开始到最后整体后移一位 
	for(int i = n; i >= x; i--){
		a[i + 1] = a[i]; // 后移 
	}
	// 2. 插入 
	a[x] = y;
	// 插入后n需要加1
	n++; 
	for(int i = 1; i <= n; i++) cout << a[i] << " "; 

    return 0; 
}

运行结果:

5
2 4 6 8 10
3 20
2 4 20 6 8 10
数组的删除

输入n个数,存入数组,找到其中元素x,并删除

#include <iostream> 
using namespace std; 

int n, a[110];  
int main() { 
	cin >> n;
	for(int i = 1; i <= n; i++) cin >> a[i];
	
	int x; cin >> x;
	// 1. 从x+1位置开始到最后整体前移一位 
	for(int i = x + 1; i <= n; i++){
		a[i - 1] = a[i]; // 前移 
	}
	// 删除后n需要减1
	n--; 
	for(int i = 1; i <= n; i++) cout << a[i] << " "; 

    return 0; 
}

运行结果:

5
2 4 6 8 10
3
2 4 8 10
数组的快速删除

输入n个数,存入数组,找到其中元素x,并快速删除

#include <iostream> 
using namespace std; 

int n, a[110];  
int main() { 
	cin >> n;
	for(int i = 1; i <= n; i++) cin >> a[i];
	
	int x; cin >> x;
	// 使用swap将删除元素换到数组最后,如果题目要求数组顺序,就不能这么做了 
	swap(a[x], a[n]);
	// 删除后n需要减1
	n--; 
	for(int i = 1; i <= n; i++) cout << a[i] << " "; 

    return 0; 
}

运行结果:

5
2 4 6 8 10
3
2 4 10 8

数组的经典应用

数组的最值查询

输入n个数,存入数组,找到其中的最大值、最小值

#include <iostream> 
using namespace std; 

int n, a[110];  
int main() { 
	cin >> n;
	for(int i = 1; i <= n; i++) cin >> a[i];
	
	int maxv = 0, minv = 0x3f3f3f3f, maxIndex = 0, minIndex = 0;
	for(int i = 1; i <= n; i++){
		if(a[i] > maxv){
			maxv = a[i];
			maxIndex = i;
		}
		if(a[i] < minv){
			minv = a[i];
			minIndex = i;
		}
	}
	
	cout << maxv << " " << maxIndex << endl;
	cout << minv << " " << minIndex << endl;

    return 0; 
}

运行结果:

5
2 4 6 8 10
10 5
2 1
数组的去重

输入n个数,存入数组,去掉重复值后打印数组

#include <iostream> 
using namespace std; 

int n, a[110], b[10010];  
int main() { 
	cin >> n;
	for(int i = 1; i <= n; i++){
		cin >> a[i];
		// 将a中所有元素值 作为b数组的下标,下标不重复
		b[a[i]] = 1; 
	}
	
	for(int i = 0; i <= 10000; i++){
		if(b[i] == 1){
			cout << i << " ";
		}
	}

    return 0; 
}

运行结果:

5
2 4 6 6 8
2 4 6 8

冒泡排序算法

冒泡排序(Bubble Sort),是一种计算机科学领域的较简单的排序算法。
它重复地走访过要排序的元素列,依次比较两个相邻的元素,如果顺序(如从小到大、首字母从Z到A)错误就把他们交换过来。走访元素的工作是重复地进行,直到没有相邻元素需要交换,也就是说该元素列已经排序完成。
这个算法的名字由来是因为越小的元素会经由交换慢慢"浮"到数列的顶端(升序或降序排列),就如同碳酸饮料中二氧化碳的气泡最终会上浮到顶端一样,故名"冒泡排序"。

实战演练:数据排序

#include <iostream> 
using namespace std; 

int n, a[110];  
int main() { 
	cin >> n;
	for(int i = 1; i <= n; i++) cin >> a[i];
	
	for(int i = 1; i < n; i++){
		if(a[i] > a[i + 1]){
			swap(a[i], a[i + 1]);
		}
	}
	
	cout << "第一趟冒泡排序的结果";
	for(int i = 1; i <= n; i++) cout << a[i] << " ";
	cout << endl;
	

    return 0; 
}

运行结果:

5
5 3 2 4 1
第一趟冒泡排序的结果3 2 4 1 5

完整代码:

#include <iostream> 
using namespace std; 

int n, a[110];  
int main() { 
	cin >> n;
	for(int i = 1; i <= n; i++) cin >> a[i];
	
	for(int j = n; j >= 2; j--){
		for(int i = 1; i < j; i++){
			if(a[i] > a[i + 1]){
				swap(a[i], a[i + 1]);
			}
		}
	}
	
	cout << "冒泡排序的结果";
	for(int i = 1; i <= n; i++) cout << a[i] << " ";
	cout << endl;
	

    return 0; 
}

运行结果:

5
5 3 2 1 4
冒泡排序的结果1 2 3 4 5
sort()

在[对数据单纯的排序]的场景下,手写排序算法的编程效率并不高,此时可以调用STL提供的算法库中的sort()来进行排序,以提高我们的编程效率。

#include <algorithm>  // 使用算法库

注:STL在语法篇的最后一章会详细介绍,这里我们只需要像调用其他函数一样直接调用sort()即可。
升序排序:

#include <iostream> 
#include <algorithm> // sort()排序 
using namespace std; 

int n, a[110];  
int main() { 
	cin >> n;
	// 数组名a代表数组中首元素的地址 
	for(int i = 1; i <= n; i++) cin >> a[i];
	// 参数1:参与排序的序列中的首元素的地址
	// 参数2:参与排序的序列中的尾元素的下一位的地址
	// [a+1, a+n+1]前闭后开,意思是,前面包含 后面不包含 
	// 参数3:比较规则,如果缺省不写,默认升序排序 
	sort(a+1, a+n+1); // sort的底层是 快速排序 
	
	for(int i = 1; i <= n; i++) cout << a[i] << " "; 

    return 0; 
}

运行结果:

5
5 4 2 3 1
1 2 3 4 5

降序排序:

#include <iostream> 
#include <algorithm> // sort()排序 
using namespace std; 

int n, a[110];  
/*回调函数
	函数以参数的方式,传到另一个函数里面,传参
	这种函数叫做回调函数 
*/
bool cmp(int A, int B){
	return A > B;
}

int main() { 
	cin >> n;
	// 数组名a代表数组中首元素的地址 
	for(int i = 1; i <= n; i++) cin >> a[i];
	// 参数1:参与排序的序列中的首元素的地址
	// 参数2:参与排序的序列中的尾元素的下一位的地址
	// [a+1, a+n+1]前闭后开,意思是,前面包含 后面不包含 
	// 参数3:比较规则,如果缺省不写,默认升序排序 
	sort(a+1, a+n+1, cmp); // sort的底层是 快速排序 
	
	for(int i = 1; i <= n; i++) cout << a[i] << " "; 

    return 0; 
}

运行结果:

5
2 4 1 3 5
5 4 3 2 1

二维数组

二维数组

定义一个2行3列的二维数组

int arr[2][3] = {
	{1, 2, 3},
	{4, 5, 6}
}
二维数组可以看成多个一维数组。
故其中2代表2个一维数组,3代表每个一维数组的最长长度为3.
#include <iostream> 
using namespace std; 

//定义一个2行3列的二维数组
int a[2][3] = {
	{1, 2, 3},
	{4, 5, 6}
}; 

int main() { 
	cout << a[0][0] << endl;
	cout << a[0][1] << endl;
	cout << a[1][2] << endl;

    return 0; 
}

运行结果:

1
2
6
二维数组的访问

二维数组的访问和一维数组的访问相似,都是通过下标去访问目标元素,二维数组行和列的下标都是从0开始的,比如想访问第i+1行的第j+1列的元素,直接访问arr[i][j]即可。

二维数组的遍历不同于一维数组,一维数组单层for循环即可完成遍历,二维数组需要用到双层for循环,通常用外层for循环遍历行的下标,内层for循环遍历列的下标。

#include <iostream> 
using namespace std; 

//定义一个2行3列的二维数组
int a[2][3] = {
	{1, 2, 3},
	{4, 5, 6}
}; 

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

    return 0; 
}

运行结果:

1 2 3
4 5 6
打印二维数组

对于一维数组的下标,数组的下标虽然从0开始,然而我们更习惯1代表第一个数,2代表第二个数,所以我们在使用数组时,往往下标0位置空着不用,真正的有效数据从下标1开始,第n个元素下标为n

那么对于二维数组也是同理,我们行和列最好也从下标1开始,更符合我们日常的思维习惯。

输入一个n行m列的数组,并打印

#include <iostream> 
using namespace std; 

int n, m, a[110][110];
int main() { 
	cin >> n >> m;
	for(int i = 1; i <= n; i++){
		for(int j = 1; j <= m; j++){
			cout << a[i][j] << " ";
		}
		cout << endl;
	} 

    return 0; 
}

运行结果:

2 5
0 0 0 0 0
0 0 0 0 0

实战演练:输入一个n行m列的矩阵A,输出它的转置。

#include <iostream> 
using namespace std; 

int n, m, a[110][110];
int main() { 
	cin >> n >> m;
	
	for(int i = 1; i <= n; i++)
		for(int j = 1; j <= m; j++)
			cin >> a[i][j];
			
	cout << "==============" << endl;
	// 固定列,打印行 
	for(int j = 1; j <= m; j++){
		for(int i = 1; i <= n; i++)
			// 列j不动,行变动 
			cout << a[i][j] << " ";
		cout << endl;
	}	
		
    return 0; 
}

运行结果:

3 3
1 2 3
4 5 6
7 8 9
==============
1 4 7
2 5 8
3 6 9
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小凡学编程

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值