C++入门基础第三篇
1.数组
042数组-一维数组定义方式
数组:所谓数组,就是一个集合,里面存放了相同类型的数据元素
- 特点1:数组中的每个数据元素都是相同的数据类型
- 特点2:数组中由连续的内存位置组成的
一维数组 一位数组的定义方式:
- 数据类型 数组名[数组长度];
- 数据类型 数组名[数组长度] = {值1,值2,......};
- 数组类型 数组名[ ] = {值1,值2,......};
#include<iostream>
#include<string>
using namespace std;
static void pritnArray(int *arr){
for (int i = 0; i < 5; i++)
cout << arr[i] << " ";
cout << endl;
}
int main()
{
//1. 数据类型 数组名[数组长度];
int arr[5];
arr[0] = 10, arr[1] = 20, arr[2] = 30, arr[3] = 40, arr[4] = 50;
pritnArray(arr);
//2. 数据类型 数组名[数组长度] = { 值1,值2,...... };
//如果在初始化数据时候,没有全部填写完成,会用0来填补剩余的数据
int arr2[5] = { 10, 20, 30, 40 };
pritnArray(arr2);
//3. 数组类型 数组名[] = { 值1,值2,...... };
int arr3[] = { 10, 20, 30, 40, 50 };
pritnArray(arr3);
system("pause");
return 0;
}
043数组-一维数组-数组名
一维数组名称的用途:
1、可以统计整个数组在内存中的长度
2、可以获取数组在内存中的首地址
#include<iostream>
#include<string>
using namespace std;
int main()
{
//数组名用途
//1、可以通过数组统计整个数组占用内存的大小
int arr[5] = { 1, 2, 3, 4, 5 };
cout << "整个数组所占内存空间:"<< sizeof(arr) << endl;
cout << "每个元素所占内存空间:"<< sizeof(arr[0]) << endl;
cout << "数组中元素个数为:" << sizeof(arr) / sizeof(arr[0]) << endl;
//2、可以通过数组名查看数组的首地址
cout << "数组的首地址为:" << (int)arr << endl;
cout << "数组中第一个元素的地址为:" << (int)&arr[0] << endl;
cout << "数组中第二个元素的地址为:" << (int)&arr[1] << endl;
//数组名是常量,不可以进行赋值操作
//arr = 100;
system("pause");
return 0;
}
044数组-一维数组案例-五只小猪称体重
练习案例1:五只小猪称体重
在一个数组中记录了五只小猪的 体重,如:int arr[5] = {300,350,200,400,250};
找出并打印最重的小猪的体重
#include<iostream>
#include<string>
using namespace std;
int main()
{
//1、创建5只小猪体重的数组
int arr[5] = { 300, 350, 200, 400, 250 };
//2、从数组中找到最大值
int max = 0; //先认定一个最大值为0
for (int i = 0; i < 5; i++){
if (max < arr[i])
max = arr[i];
}
//3、打印最大值
cout << "max = " << max << endl;
system("pause");
return 0;
}
045数组-一维数组案例-元素逆置
案例练习:数组元素逆置
案例描述:声明一个5个元素的数组,并且将元素逆置.
#include<iostream>
#include<string>
using namespace std;
int main()
{
int arr[5] = { 1, 2, 3, 4, 5 };
int start = 0; //记录起始下标位置
int end = sizeof(arr) / sizeof(arr[0]) - 1; //记录结束下标位置
for (int i = 0; i < (start + end) / 2; i++){
if (start < end){
swap(arr[start], arr[end]);
start++;
end--;
}
}
cout << "数组逆置后为:" << endl;
for (int i = 0; i < 5; i++){
cout << arr[i] << " ";
}
cout << endl;
system("pause");
return 0;
}
046数组-一维数组-冒泡排序
作用:最常用的排序算法,对数组内元素进行排序
1、比较相邻的元素,如果第一个比第二个大,就交换它们两个
2、对每一对相邻元素做同样的工作,执行完毕后,找到第一个最大值
3、重复以上的步骤,每次比较次数-1,直到不需要比较
#include<iostream>
#include<string>
using namespace std;
int main()
{
int arr[8] = { 1, 4, 5, 6, 3, 2, 8, 9 };
for (int i = 0; i < 8-1; i++){
for (int j = 0; j < 8 - i -1; j++)
swap(arr[j], arr[j + 1]);
}
for (int i = 0; i < 8; i++)
cout << arr[i] << " ";
cout << endl;
system("pause");
return 0;
}
047数组-二维数组-定义方式
二维数组就是在一维数组上,多加一个维度
二维数组定义方式
-
数据类型 数组名[行数][列数]
-
数据类型 数组名[行数][列数] = {{数据1,数据2},{数据3,数据4}};
-
数据类型 数组名[行数][列数] = { 数据1,数据2,数据3,数据4};
-
数据类型 数组名 [ ][列数] = {数据1,数据2,数据3,数据4};
#include<iostream>
#include<string>
using namespace std;
int main()
{
int arr[2][3] = { { 1, 2, 3 }, { 4, 5, 6 } };
for (int i = 0; i < 2; i++){
for (int j = 0; j < 3; j++)
cout << arr[i][j] << " ";
cout << endl;
}
system("pause");
return 0;
}
048数组-二维数组-数组名
查看二维数组所占内存空间
获取二维数组首地址
#include<iostream>
#include<string>
using namespace std;
int main()
{
int arr[2][3] = { { 1, 2, 3 }, { 4, 5, 6 } };
//1、统计查看占用内存空间大小
cout << "二维数组占用的内存空间:" << sizeof(arr) << endl;
cout << "二维数组的第一行元素所占的空间:" << sizeof(arr[0]) << endl;
cout << "二维数组第一个元素占用的内存空间:" << sizeof(arr[0][0]) << endl;
cout << "二维数组的行数为:" << sizeof(arr) / sizeof(arr[0]) << endl;
cout << "二维数组所占的列数为:" << sizeof(arr[0]) / sizeof(arr[0][0]) << endl;
//2、可以查看二维数组的首地址
cout << "二维数组首地址为:" << (int)arr << endl;
cout << "二维数组中第一行的首地址" << (int)arr[0] << endl;
cout << "二维数组中第一行第一个元素的首地址" << (int)&arr[0][0] << endl;
system("pause");
return 0;
}
049数组-二维数组案例-考试成绩统计
二维数组应用案例
考试成绩统计:
#include<iostream>
#include<string>
using namespace std;
int main()
{
//二维数组案例-考试成绩统计
//1、创建二维数组
int scores[3][3] = { { 100, 100, 100 }, { 90, 50, 100 }, { 60, 70, 80 } };
string names[3] = { "张三", "李四", "王五" };
//2、统计每个人的总分
for (int i = 0; i < 3; i++){
int sum = 0; //统计分数总和变量
for (int j = 0; j < 3; j++){
sum += scores[i][j];
}
cout <<names[i]<< "的总分为:" << sum << endl;
}
system("pause");
return 0;
}
2、 函数
050函数-函数的定义
作用:将一段经常使用的代码封装起来-减少重复代码的
一个较大的程序,一般分为若干个程序块,每个模块实现特定的功能
函数的定义
函数的定义一般主要有5个步骤:
- 1、返回值类型
- 2、函数名
- 3、参数列表
- 4、函数体语句
- 5、return 表达式
#include<iostream>
#include<string>
using namespace std;
int add(int num1, int num2){
int sum = num1 + num2;
return sum;
}
int main()
{
int a = 10, b = 20;
cout << add(a, b) << endl;
system("pause");
return 0;
}
051函数-函数的调用
- 功能:使用定义好的函数
- 语法:函数名(参数)
052函数-值传递
- 所谓值传递,就是函数调用时实参将数值传入给形参
- 值传递,如果形参发生,并不会影响实参
#include<iostream>
#include<string>
using namespace std;
//值传递
//定义函数,实现两个数字进行交换的函数
//如果函数不需要返回值,声明的时候可以写void
void swap(int a, int b){
cout << "交换前:" << endl;
cout << "a = " << a << endl;
cout << "b = " << b << endl;
int temp = a;
a = b;
b = temp;
cout << "交换后:" << endl;
cout << "a = " << a << endl;
cout << "b = " << b << endl;
}
int main()
{
int a = 10, b = 20;
cout << "main交换前:" << endl;
cout << "main a = " << a << endl;
cout << "main b = " << b << endl;
swap(a, b);
//当我们做值传递的时候,函数的形参发生改变,并不会影响实参
cout << "main交换后:" << endl;
cout << "main a = " << a << endl;
cout << "main b = " << b << endl;
system("pause");
return 0;
}
053函数-常见的样式
常见的函数样式有4种
- 1、无参无返
- 2、有参无返
- 3、无参有返
- 4、有参有返
#include<iostream>
#include<string>
using namespace std;
//函数的常见样式
static void test01(){//1、无参无返
cout << "this is test01()" << endl;
}
static void test02(int a){//2、有参无返
cout << "this is test02 a = " << a << endl;
}
static int test03(){//3、无参有返
cout << "this is test03()" << endl;
return 12;
}
static int test04(int a){//4、有参有返
cout << "this is test04 a = " << a << endl;
return 0;
}
int main()
{
test01();
test02(10);
int num1 =test03();
cout << num1 << endl;
test04(20);
system("pause");
return 0;
}
054函数-函数的声明
作用:告诉编译器函数名称及如何调用函数,函数的实际主体可以单独定义 函数的声明可以多次,但函数的定义只能有一次。
#include<iostream>
#include<string>
using namespace std;
//函数声明
//比较函数,实现两个整型的数字进行比较,返回较大的值
//提前告诉编译器函数的存在,可以利用函数的声明
//声明可以有多次
extern int max(int a, int b);
extern int max(int a, int b);
extern int max(int a, int b);
int main()
{
int a = 10, b = 20;
cout << max(a, b)<<endl;
system("pause");
return 0;
}
extern int max(int a, int b){
return a > b ? a : b;
}
055函数-函数的分文件编写
作用:让代码结构更加清晰
函数分文件编写一本有4个步骤
-
- 创建后缀名为.h的头文件
-
- 创建后缀名为.cpp的源文件
-
- 在头文件种写函数的声明
-
- 在源文件中写函数的定义
055myswap.h
#include<iostream>
using namespace std;
//函数的声明
void swap(int a, int b);
055myswap.cpp
#include"055myswap.h"
//函数的定义
void swap(int a, int b){
int temp = a;
a = b;
b = temp;
cout << "a = " << a << endl;
cout << "b = " << b << endl;
}
055.cpp
#include<iostream>
#include<string>
#include"055myswap.h"
using namespace std;
//函数的分文件编写
//实现一个两个数字交换的函数
int main()
{
int a = 10, b = 20;
swap(a, b);
system("pause");
return 0;
}