从零开始的C++学习(7)

五、数组

5.1 概述

所谓数组,间就是一个集合,里面存放了相同类型的数据元素

特点1:数组中的每个数据元素都是相同的数据类型

特点2:数组是由连续的内存位置组成的


5.2 一维数组

5.2.1 一维数组定义方式

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

  1. 数据类型 数组名[ 数组长度 ];

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

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

数组的下标都是从零开始的!

第一种:

#include <iostream>
using namespace std;

int main()
{
	//数组

	/*
	数据类型 数组名[ 数组长度 ];
	数据类型 数据名[ 数据长度 ] = {值1,值2,……};
	数据类型 数据名[ ] = { 值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;

	system("pause");

	return 0;
}

第二种:

#include <iostream>
using namespace std;

int main()
{
	//数组

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

	//第二种
	int arr[5] = { 10,20,30,40,50 };
	cout << arr[0] << endl;
	cout << arr[1] << endl;
	cout << arr[2] << endl;
	cout << arr[3] << endl;
	cout << arr[4] << endl;


	system("pause");

	return 0;
}

#include <iostream>
using namespace std;

int main()
{
	//数组

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

	//第二种 简化版
	int arr[5] = 
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值