数组
数组就是一个集合,里面存放了相同类型的数据元素
特点:数组中给个元素都是相容的数据类型,在内存中连续存放
一维数组的的三种定义方式:
1. 数据类型 数组名[数组长度]
2. 数据类型 数组名[数组长度] = {值1, 值2, 值3}
3. 数组类型 数组名[] = {值1, 值2, 值3…}
#include<iostream>
using namespace std;
// time 系统时间头文件包含
#include<ctime>
int main()
{
// 1. 数据类型 数组名[数组长度]
int arr[5];
arr[0] = 10;
arr[1] = 20;
arr[2] = 30;
arr[3] = 40;
// 访问数组元素
cout << arr[0] << endl;
cout << arr[0] << endl;
// 2. 数据类型 数组名[数组长度] = {值1, 值2, 值3}
// 如果在初始化数据的时候,没有全部填写,会用0填补
int arr2[5] = {10, 20, 30, 40, 50};
//cout << arr[1] << endl;
//cout << arr[2] << endl;
for (int i=0; i < 5; i++)
{
cout << arr2[i] << endl;
}
// 3. 数组类型 数组名[] = {值1, 值2, 值3...}
// 定义数组的时候,必须有定义初始的长度
int arr3[] = {1, 2, 3, 4, 5, 6, 7, 8};
for (int i = 0; i < 5; i++)
{
cout << arr3[i] << endl;
}
system("pause");
return 0;
}
2.一维数组名的用处
可以统计整个数组在内存中的长度,可以获取数组在内存中的首地址
#include<iostream>
using namespace std;
// time 系统时间头文件包含
#include<ctime>
int main()
{
// 数组名的用途,
// 1. 可以通过数组名