【C++学习】数组(一)——一维数组

【C++学习】数组(一)——一维数组一 概念 数组是具有一定顺序关系的若干对象的结合体 组成数组的对象称为该数组的元素 同一数组的各元素具有相同的类型 每个元素有 n 个下标的数组称为 n 维数组 1 1 一维数组定义方式 数据类型 数组名 数组长度 数据类型 数组名 数组长度 值 1 值 2 数据类型 数组名 值 1 值 2 1 2 特点

大家好,我是讯享网,很高兴认识大家。

一、概念

数组是具有一定顺序关系的若干对象的结合体,组成数组的对象称为该数组的元素。同一数组的各元素具有相同的类型。每个元素有n个下标的数组称为n维数组。

1.1 一维数组定义方式

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

1.2 特点

  1. 放在一块连续的内存空间中。
  2. 数组中每个元素都是相同数据类型


讯享网

 用array来命名一个一维数组,且其下标为从0到N的整数

二、一维数组的使用

2.1 声明方式

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

第一种:

 //数据类型 数组名 数组长度 int arr[5]; //给数组中的元素进行赋值 arr[0] = 10; arr[1] = 20; arr[2] = 30; arr[3] = 40; arr[4] = 50; //访问数据元素 cout << arr[0] << endl; cout << arr[1] << endl; cout << arr[2] << endl; cout << arr[3] << endl; cout << arr[4] << endl;

讯享网

第二种:

讯享网 int arr[5] = { 10,20,30,40,50 }; //print all the elements in the array in a loop for (int i = 0; i < 5; i++) { cout << arr[i] << endl; }

如果在初始化数据的时候,没有全部填写完,会用0来填补剩余数据。

 int arr[5] = { 10,20,30 }; //print all the elements in the array in a loop for (int i = 0; i < 5; i++) { cout << arr[i] << endl; }

运行结果:

第三种:

讯享网 //定义数组时,必须有初始长度 int arr[] = {10,20,30,40,50,60}; for (int i = 0;i < 6;i++) { cout<<arr[i]<<endl; }

2.2 一维数组的名称

用途:

  • 可以统计整个数组在内存中的长度
  • 可以获取数组在内存中的首地址

The first address of the array:(int)arr The location of the first element in the array:(int)&arr[0] The location of the second element in the array:(int)&arr[1]

 数组名是常量,不可以进行赋值操作。

三、实例

3.1 判断数组中元素的最大值

讯享网 int max = 0;//let the maximum value be 0 int arr[5] = { 300,350,200,400,250 }; for (int i = 0; i < 5; i++) { //If the array element is bigger than the recognized maximum value, then update the maximum value if (arr[i] > max) { max = arr[i]; } } cout << max;

3.2 数组元素的逆置

说明:若要交换起始位置和结束位置的元素,则首先创建一个临时内存空间储存起始位置的元素,然后用结束位置的元素覆盖起始位置的元素,再用临时位置的元素覆盖结束位置的元素。

 int array[5] = { 1,3,4,2,5 }; //note the location of start subscript and end subscript int start = 0; int end = sizeof(array) / sizeof(array[0]) - 1; //exchange the start element and the end element // start++,end-- //do the exchange in a loop until start>=end while (start < end) { //exchange the elements int temp = array[start]; array[start] = array[end]; array[end] = temp; //update the subscripts start++; end--; } for (int i = 0; i < 5; i++) { cout << array[i] << endl; } }

四、冒泡排序

最常用的排序算法,对数组内元素进行排序。

1.比较相邻的元素,如果第一个比第二个大,则交换他们两个。

2.对每一对相邻元素做同样的工作,执行完毕后,找到第一个最大值。

3.重复以上的步骤,每次比较次数-1,直到不需要比较。

借用黑马程序员的教程:

 代码实现:

讯享网int main() { int array[9] = { 4,2,8,0,5,7,1,3,9 }; cout << "Unsorted elements:" << endl; for (int i = 0; i < 9; i++) { cout << array[i] << " "; } //Start Bubble sort(冒泡排序) //Sorting rounds=Number of elements -1 //Number of comparisons in each round =Number of elements-Sorting rounds-1 for (int i = 0; i < 9 -i- 1; i++) { //The comparison of Inner loop for (int j = 0; j < 9 - 1; j++) { //If the first number is lager than the second number,exchange them if (array[j] > array[j + 1]) { int temp = array[j]; array[j] = array[j+1]; array[j+1] = temp; } } } cout << endl; cout << "Sorted elements:" << endl; for (int i = 0; i < 9; i++) { cout << array[i] << " "; } }

小讯
上一篇 2025-03-20 11:36
下一篇 2025-03-23 13:48

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/89796.html