C++基础入门学习笔记

00 输出Hello world与注释

1.C++的基本框架

在书写C++代码时,都可以用以下基本框架

include<iostream>
using namespace std;
int main()
{

system("pause");
return 0;
}

2.输出hello world

include<iostream>
using namespace std;

int main()
{
cout<<"hello world"<<endl;
system("pause");
return 0;
}

3.注释

作用:在代码中加一些说明和解释,方便自己或其他程序员阅读代码

包括单行注释和多行注释两种类型

#include<iostream>
using namespace std;
/* 
	main函数是一个程序的入口
	每个程序必须有main函数
	有且仅有一个
*/
int main()
{
	// 在屏幕中输出hello world
	cout << "hello world" << endl;
	system("pause");
	return 0;
}

// 1.单行注释,通常放在一行代码的上方,或者一条语句的末尾,对该行代码说明
/*
	2.多行注释
	通常放在一段代码的上方
	对该段代码做整体说明
*/

01 变量与常量

1.变量

给一段指定的内存空间起名,方便操作这段内存

语法:数据类型 变量名 = 变量初始值

#include<iostream>
using namespace std;

int main()
{
//创建变量
int a = 10;
//输出 a = 10
cout << "a = "<< a << endl;

system("pause");
return 0;
}

2.常量

C++定义常量的两种方式:

(1)#define宏常量:#define 常量名 常量值

通常在文件上方定义,表示一个常量

(2)const 修饰的常量:const 数据类型 常量名 = 常量值;

通常在变量定义前加关键字const,修饰该变量为常量,不可修改

#include<iostream>
using namespace std;

//define定义的宏常量
 #define Day 7

int main()
{
cout << "一周总共有" << Day "天" << endl;

//const 修饰的常量
const int month = 12
cout << "一年总共有" << month << "个月" << endl;

system("pause");
return 0;
}

02 关键字与标识符命名规则

1.关键字

关键字是C++中保留的单词

在定义变量或者常量时,不可使用关键字

2.标识符命名规则

(1)标识符不可以是关键字

(2)标识符只能由字母、数字或下划线组成

(3)第一个字符必须为字母或下划线

(4)标识符中的字母区分大小写

在给变量起名时,最好能够做到见名知意

03 数据类型

1.整型

整型变量表示的是整数类型的数据

分为以下四种类型,区别在于所占内存空间不同:

short(短整型):2字节

int(整型):4字节

long(长整型):Windows为4字节,Lniux32位4字节,64位8字节

long long(长长整型):8字节

#include<iostream>
using namespace std;

int main()
{
	//短整型
	short num1 = 10;
	//整型
	int num2 = 10;
	//长整型
	long num3 = 10;
	//长长整型
	long long num4 = 10;

	cout << "num1 = " << num1 << endl;
	cout << "num2 = " << num1 << endl;
	cout << "num3 = " << num1 << endl;
	cout << "num4 = " << num1 << endl;

	system("pause");
	return 0;
}

2.sizeof关键字

可以利用sizeof求出数据类型所占内存大小

语法:sizeof(数据类型)或sizeof(变量)

# include<iostream>
using namespace std;

int main()
{
	short num1 = 10;
	cout << "num1所占内存空间为:" << sizeof(short) << endl;

	int num2 = 10;
	cout << "num2所占内存空间为:" << sizeof(int) << endl;

	long num3 = 10;
	cout << "num3所占内存空间为:" << sizeof(long) << endl;

	long long num4 = 10;
	cout << "num4所占内存空间为:" << sizeof(long long) << endl;

	system("pause");
	return 0;
}

3.浮点型(实型)

用于表示小数

分为两种:

单精度:float

双精度:double

两者区别在于表示的有效数字范围不同

float:7位有效数字

double:15-16位有效数字

默认情况下,输出一个小数,会显示出6位有效数字

# include<iostream>
using namespace std;

int main()
{
	//编译器会默认小数为双精度,单精度在后面要加一个f
	float f1 = 3.14f;
	cout << "f1 = " << f1 << endl;
	cout << "f1的所占的内存空间为:" << sizeof(float) << endl;

	double d1 = 3.14;
	cout << "d1 = " << d1 << endl;
	cout << "d1所占的内存空间为:" << sizeof(double) << endl;

	//科学计数法
	float f2 = 3e2;//3*10^2
	float f3 = 3e-2;//3*10^-2
	cout << "f2 = " << f2 << endl;
	cout << "f3 = " << f3 << endl;

	system("pause");
	return 0;
}

4.字符型

用于显示单个字符

语法:char ch = 'a';

在定义字符型变量时,用单引号将字符括起来,不可以用双引号

单引号内只能有一个字符,不可以是字符串

字符型变量只占用1个字节

字符型变量并不是把字符本身放到内存中存储,而是将对应的ASCII码放入到存储单元

# include<iostream>
using namespace std;

int main()
{
	char ch = 'a';
	cout << ch << endl;
	cout << "ch所占内存大小:" << sizeof(char) << endl;
	//ch对应的ASCII码,(int)ch表示将字符型强制转换成整型
	cout << (int)ch << endl;

	system("pause");
	return 0;
}

5.转义字符

用于表示一些不能显示出来的ASCII字符

常用:\n(换行符)

 \\   一个反斜线\

 \t(制表符Tab)

# include<iostream>
using namespace std;

int main()
{
	cout << "hello world\n";
	cout << "\\" << endl;
	cout << "aaa\thello world" << endl;

	system("pause");
	return 0;
}

6.字符串型

用于表示一串字符

两种风格:

C风格:char 变量名[] = "字符串值";

C++风格:string 变量名 = "字符串值";

C++风格需要包含头文件string:# include<string>

# include<iostream>
using namespace std;
# include<string>

int main()
{
	char str1[] = "hello world";
	cout << str1 << endl;

	string str2 = "hello kitty";
	cout << str2 << endl;

	system("pause");
	return 0;
}

7.布尔类型

代表真或假的值

bool类型只有两个值:

true——真(本质是1)

false——假(本质是0)

布尔类型只要是非0的值就为1

布尔类型占一个字节大小

# include<iostream>
using namespace std;

int main()
{
	bool flag = true;
	cout << flag << endl;

	flag = false;
	cout << flag << endl;

	cout << "bool类型所占内存空间:" << sizeof(bool) << endl;

	system("pause");
	return 0;
}

8.数据的输入

用于从键盘获取数据

关键字:cin

语法:cin >> 变量

# include<iostream>
using namespace std;

int main()
{
	int a = 0;
	float f = 0.0;
	char ch = 'a';
	bool flag = false;

	cout << "请给整型a赋值" << endl;
	cin >> a;
	cout << "整型 a = " << a << endl;

	cout << "请给浮点型f赋值" << endl;
	cin >> f;
	cout << "浮点型 f = " << f << endl;

	cout << "请给字符型ch赋值" << endl;
	cin >> ch;
	cout << "字符型 ch = " << ch << endl;

	cout << "请给字符型flag赋值" << endl;
	cin >> flag;
	cout << "字符型 flag = " << flag << endl;

	system("pause");
	return 0;
}

04 运算符

用于执行代码的运算

1.算术运算符

用于处理四则运算

包括:

+(加或正),-(减或负),*(乘),\(除)

%(取模或取余)

++(自增,包括前置和后置),--(自减,包括前置和后置)

(1)四则运算

# include<iostream>
using namespace std;

int main()
{
	int a1 = 10;
	int b1 = 3;
	cout << a1 + b1 << endl;
	cout << a1 - b1 << endl;
	cout << a1 * b1 << endl;
	cout << a1 / b1 << endl;

	// 两个整数相除,结果依然是整数,直接去掉小数部分
	int a2 = 10;
	int b2 = 20;
	cout << a2 / b2 << endl;

	// 两个小数可以相除,结果可以是小数(整除为整数)
	double d1 = 0.5;
	double d2 = 0.22;
	cout << d1 / d2 << endl;

	system("pause");
	return 0;
}

(2)取模运算

即为求余数

如:10 /3 = 3 …… 1,1即为余数,则10 % 3 = 1

两个数相除,0不能做除数,同样也无法进行取模运算

两个小数不可以进行取模运算

# include<iostream>
using namespace std;

int main()
{
	int a1 = 10;
	int b1 = 3;
	cout << 10 % 3 << endl;

	int a2 = 10;
	int b2 = 20;
	cout << a2 / b2 << endl;

	system("pause");
	return 0;
}

(3)自增自减

自增:让变量+1

自减:让变量-1

自增包括前置自增和后置自增,前置自增先让变量+1,然后进行表达式的运算;后置自增先进行表达式的运算,然后让变量+1

同理,自减包括前置自减和后置自减,前置自减先让变量-1,然后进行表达式的运算;后置自减先进行表达式的运算,然后让变量-1

# include<iostream>
using namespace std;

int main()
{
	int a = 10;
	a++;
	cout << a << endl;

	int b = 10;
	++b;
	cout << b << endl;

	int a1 = 10;
	int b1 = 10;
	int c1;
	c1 = a1++ * b1;
	cout << "a1 = " << a1 << endl;
	cout << "c1 = " << c1 << endl;

	int a2 = 10;
	int b2 = 10;
	int c2;
	c2 = ++a2 * b2;
	cout << "a2 = " << a2 << endl;
	cout << "c2 = " << c2 << endl;

	system("pause");
	return 0;
}

2.赋值运算符

将表达式的值赋给变量

包括:=(等于),+=(加等于,先加后赋值),-=(减等于,先减后赋值),*=(乘等于,先乘后赋值),/=(除等于,先除后赋值),%=(模等于,先取模后赋值)

# include<iostream>
using namespace std;

int main()
{
	int a = 10;
	a = 100;
	cout << "a = " << a << endl;

	a = 10;
	a += 2; // a = a + 2
	cout << "a = " << a << endl;

	a = 10;
	a -= 2; // a = a - 2
	cout << "a = " << a << endl;

	a = 10;
	a *= 2; // a = a * 2
	cout << "a = " << a << endl;

	a = 10;
	a /= 2; // a = a/2
	cout << "a = " << a << endl;

	a = 10;
	a %= 2; // a = a % 2
	cout << "a = " << a << endl;

	system("pause");
	return 0;
}

3.比较运算符

包括:==(相等于),!=(不等于),<(小于),>(大于),<=(小于等于),>=(大于等于)

比较的结果为真或假(1或0)

# include<iostream>
using namespace std;

int main()
{
	int a = 10;
	int b = 20;

	cout << (a == b) << endl; // 这里涉及到优先级,比较运算符优先级较低,需要加括号先运算
	cout << (a != b) << endl;
	cout << (a < b) << endl;
	cout << (a > b) << endl;
	cout << (a <= b) << endl;
	cout << (a >= b) << endl;

	system("pause");
	return 0;
}

4.逻辑运算符

包括:!(非),&&(与),||(或)

(1)逻辑非

!a:若a为真,则!a为假;若a为假,则!a为真

注:在C++中,除了0都为真

# include<iostream>
using namespace std;

int main()
{
	int a = 10;
	cout << !a << endl;
	cout << !!a << endl;

	system("pause");
	return 0;
}

(2)逻辑与

同真为真,其余为假

# include<iostream>
using namespace std;

int main()
{
	int a = 10;
	int b = 10;
	cout << (a && b) << endl;

	b = 0;
	cout << (a && b) << endl;

	a = 0;
	b = 0;
	cout << (a && b) << endl;

	system("pause");
	return 0;
}

(3)逻辑或

同假为假,其余为真

# include<iostream>
using namespace std;

int main()
{
	int a = 10;
	int b = 10;
	cout << (a || b) << endl;

	a = 0;
	b = 10;
	cout << (a || b) << endl;

	a = 0;
	b = 0;
	cout << (a || b) << endl;

	system("pause");
	return 0;
}

05 程序流程结构

包括以下三种:

顺序结构:程序按照顺序执行,不发生跳转

选择结构:依据条件是否满足,有选择地执行相应功能

循环结构:依据条件是否满足,循环多次执行某段代码

1.选择结构

(1)if语句

①单行if语句

语法:if(条件){条件满足执行的语句}

注:if条件后面不要加分号

<案例>

输入一个分数,若分数大于85,则成绩优秀,在屏幕上输出

# include<iostream>
using namespace std;

int main()
{
	// 输入分数
	int score;
	cout << "请输入一个分数:" << endl;
	cin >> score;

	// 提示用户输入的分数
	cout << "您输入的分数为:" << score << endl;

	// 判断分数是否符合条件
	if (score > 85)
	{
		cout << "成绩优秀" << endl;
	}

	system("pause");
	return 0;
}

②多行if语句

语法:if(条件){条件满足执行的语句}else{条件不满足执行的语句}

<案例>

输入考试分数,若大于等于60,则为及格;若小于60,则为不及格

# include<iostream>
using namespace std;

int main()
{
	// 输入考试分数
	int score;
	cout << "请输入一个分数:" << endl;
	cin >> score;

	// 提示输入的分数
	cout << "您输入的分数为:" << score << endl;

	// 判断分数是否大于60
	if (score >= 60)
	{
		cout << "成绩及格" << endl;
	}
	else
	{
		cout << "成绩不及格" << endl;
	}

	system("pause");
	return 0;
}

③多条件if语句

语法:if(条件1){条件1满足执行的语句}else if(条件2){条件2满足执行的语句}……else{都不满足执行的语句}

<案例>

输入一个分数:若大于85,则为优秀;若大于75,则为良好;若大于60,则为及格;以上都不满足,则为不及格

# include<iostream>
using namespace std;

int main()
{
	// 输入分数
	int score;
	cout << "请输入一个分数:" << endl;
	cin >> score;

	// 提示用户输入的分数
	cout << "您输入的分数为:" << endl;

	// 判断
	if (score > 85)
	{
		cout << "成绩优秀" << endl;
	}
	else if (score > 75)
	{
		cout << "成绩良好" << endl;
	}
	else if (score > 60)
	{
		cout << "成绩及格" << endl;
	}
	else
	{
		cout << "成绩不及格" << endl;
	}

	system("pause");
	return 0;
}

④嵌套if语句

<案例>

输入一个分数:如果大于60,成绩及格;如果小于60,成绩优秀;在及格的成绩中,大于70为成绩良好,大于85为成绩优秀

# include <iostream>
using namespace std;

int main()
{
	// 输入分数
	int score;
	cout << "请输入一个分数:" << endl;
	cin >> score;

	// 提示用户输入的分数
	cout << "您输入的分数为:" << endl;

	// 判断
	if (score > 60)
	{
		cout << "成绩及格" << endl;
		if (score > 70)
		{
			cout << "恭喜成绩良好" << endl;
		}
		if (score > 85)
		{
			cout << "恭喜成绩优秀" << endl;
		}
	}
	if (score < 60)
	{
		cout << "成绩不及格" << endl;
	}

	system("pause");
	return 0;
}

<案例>

三只小猪称体重:

有三只小猪ABC,分别输入三只小猪的体重,并判断哪只小猪最重

 include<iostream>
using namespace std;

int main()
{
	// 创建三只小猪的体重变量
	int a = 0;
	int b = 0;
	int c = 0;

	// 输入三只小猪的体重
	cout << "请输入小猪A的体重" << endl;
	cin >> a;
	cout << "请输入小猪B的体重" << endl;
	cin >> b;
	cout << "请输入小猪C的体重" << endl;
	cin >> c;

	// 提示输入的三只小猪的体重
	cout << "小猪A的体重为" << a << endl;
	cout << "小猪B的体重为" << b << endl;
	cout << "小猪C的体重为" << c << endl;

	// 比较体重
	// 首先比较A与B谁重
	if (a > b) // A比B重
	{
		if (a > c) // A比B重且A比C重
		{
			cout << "小猪A最重" << endl;
		}
		if(a < c) // A比B重但C比A重
		{
			cout << "小猪C最重" << endl;
		}
	}
	if (a < b) // B比A重
	{
		if (b > c) // B比A重且B比C重
		{
			cout << "小猪B最重" << endl;
		}
		if(b < c) // B比A重但C比B重
		{
			cout << "小猪C最重" << endl;
		}
	}
	

	system("pause");
	return 0;
}

(2)三目运算符

通过三目运算符实现简单的判断

语法:表达式1?表达式2:表达式3

如果表达式1为真,执行表达式2,返回表达式2的结果;否则执行表达式3,返回表达式3的结果

注:C++中三目运算符返回的是变量,可以继续赋值

# include<iostream>
using namespace std;

int main()
{
	// 创建三个变量:a,b,c
	int a = 10;
	int b = 20;
	int c = 0;
	// 将a与b进行比较,较大者赋值给c
	c = (a > b ? a : b);
	cout << "c = " << c << endl;

	//C++中三目运算符返回的是变量,可以继续赋值
	(a < b ? a : b) = 100;
	cout << "a = " << a << endl;
	cout << "b = " << b << endl;

	system("pause");
	return 0;
}

(3)switch语句

执行多条件分支语句

语法:

switch(表达式)

{

case结果1:执行语句;break;

case结果2:执行语句;break;

……

defult:执行语句;break;

}

缺点:switch判断时,只能是整型或字符型,不能是一个区间

优点:结构清晰,执行效率高

<案例>

给电影打分:

9~10:经典;7~8:非常好;5~6:一般;5以下:烂片

# include<iostream>
using namespace std;

int main()
{
	int score = 0;
	cout << "请给电影打分:" << endl;
	cin >> score;

	switch (score)
	{
	case 10:
		cout << "您认为这是经典电影" << endl;
		break; // 跳出当前分支
	case 9:cout << "您认为这是经典电影" << endl;break;
	case 8:cout << "您认为电影非常好" << endl; break;
	case 7:cout << "您认为电影非常好" << endl; break;
	case 6:cout << "您认为电影一般" << endl; break;
	case 5:cout << "您认为电影一般" << endl; break;
	defult:cout << "您认为这是烂片" << endl; break;
	}

	system("pause");
	return 0;
}

2.循环结构

(1)while语句

语法:while(循环条件){循环语句}

只要循环条件的结果为真,就执行循环语句

注:在循环中可以用break关键字来退出当前循环

<案例>输出0~9

# include<iostream>
using namespace std;

int main()
{
	int num = 0;
	// 当num小于10,输出num,然后对num+1
	while (num < 10)
	{
		cout << num << endl;
		num++;
	}

	system("pause");
	return 0;
}

<案例>

猜数字

系统随机生成一个1到100间的数字,玩家进行猜测,如果猜错,提示玩家数字过大或过小,如果猜对恭喜玩家胜利,并且退出游戏

# include <iostream>
using namespace std;
//包含time系统时间头文件
# include<ctime>

int main()
{
	// 添加随机数种子:利用当前系统时间生成随机数,防止每次生成的随机数都一样
	srand((unsigned int)time(NULL));

	// 生成随机数
	// rand()函数:rand()%100 会生成0-99的随机数;rand()%100 + 1 生成的是0 + 1 - 99 + 1(0-100)的随机数
	int num = rand() % 100 + 1;

	int val = 0;
	while (1)
	{
		cin >> val;
		if (val < num)
		{
			cout << "猜测过小" << endl;
		}
		else if (val > num)
		{
			cout << "猜测过大" << endl;
		}
		else
		{
			cout << "恭喜您猜对了" << endl; break;//利用break退出当前循环
		}

	}

	system("pause");
	return 0;
}

(2)do...while语句

语法:do{循环语句}while(循环条件);

与while的区别在于do...while会先执行一次循环语句,再判断循环条件

<案例>

输出0~9

# include<iostream>
using namespace std;

int main()
{
	int num = 0;

	do
	{
		cout << num << endl;
		num++;
	} while (num < 10);

	system("pause");
	return 0;
}

<案例>

水仙花数

水仙花数是指一个三位数,其每个数位上的数字的3次幂之和等于这个数本身

例:1^3+5^3+3^3=153,则153为水仙花数

# include<iostream>
using namespace std;

int main()
{
	// 先找到所有的三位数
	int num = 100;

	int a = 0; // 个位
	int b = 0; // 十位
	int c = 0; // 百位

	do
	{
		a = num % 10; // 取模于10,获取个位
		b = num / 10 % 10; // 先除10,再取模于10,获取十位
		c = num / 100; // 除100,获取百位

		if (a * a * a + b * b * b + c * c * c == num)
		{
			cout << num << endl;
		}
				
		num++;
	} while (num < 1000);
	system("pause");
	return 0;
}

(3)for循环

语法:for(起始表达式;条件表达式;末尾循环体){循环语句;}

起始表达式只执行一次,判断条件表达式,符合条件执行循环语句,然后执行末尾循环体,再判断条件表达式

<案例>

输出0~9

# include<iostream>
using namespace std;

int main()
{
	for (int i = 0; i < 10; i++)
	{
		cout << i << endl;
	}

	system("pause");
	return 0;
}

<案例>

敲桌子

从数字1开始数到数字100,如果个位数字或十位数字含有7,或者该数字是7的倍数,输出“敲桌子”,其余数字直接输出

# include<iostream>
using namespace std;

int main()
{
	for (int i = 1; i <= 100; i++)
	{
		if (i % 7 == 0|| i % 10 == 7|| i / 10 == 7) // 7的倍数,个位7,十位7
		{
			cout << "敲桌子" << endl;
		}
		else
		{
			cout << i << endl;
		}
	}
	system("pause");
	return 0;
}

(4)嵌套循环

在循环体中再嵌套一层循环

外层执行一次,内层执行一周

<案例>

打印星图(10×10)

# include<iostream>
using namespace std;

int main()
{
	for (int i = 0; i < 10; i++)
	{
		for (int j = 0; j < 10; j++)
		{
			cout << "* ";
		}
		cout << endl;
	}

	system("pause");
	return 0;
}

<案例>

乘法口诀表

# include<iostream>
using namespace std;

int main()
{
	for (int i = 1; i <= 9; i++)
	{
		for (int j = 1; j <= i; j++)
		{
			cout << j << "*" << i << "=" << j * i << "  ";
		}
		cout << endl;
	}

	system("pause");
	return 0;
}

3.跳转语句

(1)break语句

用于跳出选择结构或循环结构

使用时机:

①switch条件语句中,终止case并跳出switch

# include<iostream>
using namespace std;

int main()
{
	cout << "请选择副本的难度:" << endl;

	cout << "1.普通" << endl;
	cout << "2.中等" << endl;
	cout << "3.困难" << endl;

	int select = 0;
	cin >> select;

	switch (select)
	{
	case 1:cout << "您选择的是普通难度" << endl; break;
	case 2:cout << "您选择的是中等难度" << endl; break;
	case 3:cout << "您选择的是困难难度" << endl; break;
	default:break;
	}

	system("pause");
	return 0;
}

②循环语句中,跳出当前的循环语句

# include<iostream>
using namespace std;

int main()
{
	for (int i = 0; i < 10; i++)
	{
		//如果i=5,退出循环,不再打印
		if (i == 5)
		{
			break;
		}
		cout << i << endl;
	}

	system("pause");
	return 0;
}

③嵌套循环中,跳出最近的内层循环语句

# include<iostream>
using namespace std;

int main()
{
	for (int i = 0; i < 10; i++)
	{
		for (int j = 0; j < 10; j++)
		{
			if (j == 5)
			{
				break;
			}
			cout << "* ";
		}
		cout << endl;
	}

	system("pause");
	return 0;
}

(2)continue语句

在循环语句中,跳过本次循环余下尚未执行的部分,继续执行下一次循环

# include<iostream>
using namespace std;

int main()
{
	for (int i = 0; i <= 20; i++)
	{
		// 只输出奇数
		if (i % 2 == 0)
		{
			continue;
		}
		cout << i << endl;
	}


	system("pause");
	return 0;
}

(3)goto语句

(不常用,会显得混乱)

可以无条件跳转语句

语法:goto 标记;

如果标记的名称存在,执行到goto语句时,会跳转到标记的位置

# include<iostream>
using namespace std;

int main()
{
	cout << "1.××××" << endl;
	cout << "2.××××" << endl;
	goto FLAG;
	cout << "3.××××" << endl;
	cout << "4.××××" << endl;
	FLAG: //标记后要加冒号
	cout << "5.××××" << endl;

	system("pause");
	return 0;
}

06 数组

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

两个特点:

(1)数组中每个元素都是相同的数据类型

(2)数组由连续的内存位置组成

1.一维数组

(1)定义

三种定义方式:

①数据类型 数组名[数组长度];

可以通过下标访问数组中的元素,注意数组的下标从0开始

# include<iostream>
using namespace std;

int main()
{
	int arr[5];

	// 通过下标给数组赋值(注意下标从0开始)
	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;
}

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

如果在定义时,值的个数小于数组长度,会用0代替剩余的位置

# include<iostream>
using namespace std;

int main()
{
	int arr[5] = { 10,20,30};
	// 可以利用循环进行输出,使代码更简洁
	for (int i = 0; i < 5; i++)
	{
		cout << arr[i] << endl;
	}

	system("pause");
	return 0;
}

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

注意:定义数组的时候,必须要有初始长度(中括号和花括号里的内容不能同时省略)

# include<iostream>
using namespace std;

int main()
{
	int arr[] = { 90,80,70,60,50,40,30,20,10 };
	for (int i = 0; i < 9; i++)
	{
		cout << arr[i] << endl;
	}

	system("pause");
	return 0;
}

(2)数组名

两个用途:

①统计整个数组所占内存空间的大小(通过sizeof函数)

②获取数组在内存中的首地址

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

# include<iostream>
using namespace std;

int main()
{
	int arr[] = { 1,2,3,4,5,6,7,8,9,10 };

	// 统计整个数组所占内存空间的大小
	cout << "整个数组在内存所占内存空间的大小:" << sizeof(arr) << endl;
	cout << "每个数组元素所占内存空间的大小:" << sizeof(arr[0]) << endl;
	cout << "数组的元素个数:" << sizeof(arr) / sizeof(arr[0]) << endl;
	// 获取数组在内存中的首地址
	cout << "数组的首地址:" << (int)arr << endl;
	// &为取址符,可以获取元素的地址
	cout << "数组中第一个元素的地址:" << (int)&arr[0] << endl;
	cout << "数组中第二个元素的地址:" << (int)&arr[1] << endl;

	system("pause");
	return 0;
}

<案例>

五只小猪称体重

在一个数组中记录了五只小猪的体重找到并输出最重的小猪的体重

# include<iostream>
using namespace std;

int main()
{
	// 创建五只小猪的体重
	int arr[] = { 300,350,500,400,250 };

	// 找到最大体重
	int max = 0; //首先指定最大体重为max=0
	for (int i = 0; i < 5; i++)
	{
		// 如果数组中元素的值大于max,则更新max
		if (max < arr[i])
		{
			max = arr[i];
		}
	}

	// 输出最大体重
	cout << "五只小猪的最大体重为:" << max << endl;

	system("pause");
	return 0;
}

<案例>

元素逆置

例:原数组元素为1,2,3,4,5;逆置后输出结果为5,4,3,2,1

# include<iostream>
using namespace std;

int main()
{
	int arr[] = { 1,3,2,5,4 };
	// 逆置前进行输出
	cout << "逆置前:";
	for (int i = 0; i < 5; i++)
	{
		cout << arr[i] << ", ";
	}

	cout << endl;

	int start = 0; // 起始元素下标
	int end = sizeof(arr) / sizeof(arr[0]) - 1; // 末尾元素下标
	int temp; // 临时元素

	// 将元素进行逆置
	while(start<end)
	{
		temp = arr[start]; // 先利用temp存放arr[start]的值
		arr[start] = arr[end]; // 将arr[end]的值赋给arr[start]
		arr[end] = temp; // 将temp即arr[start]的初始值赋给arr[end]

		start++;
		end--;
	}

	// 逆置后进行输出
	cout << "逆置后:";
	for (int i = 0; i < 5; i++)
	{
		cout << arr[i] << ", ";
	}

	cout << endl;

	system("pause");
	return 0;
}

(3)冒泡排序

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

分为以下三个步骤:

①比较相邻的元素,如果第一个比第二个大,就交换他们两个

②对每一相邻元素做同样的工作,找到第一个最大值

③重复以上步骤,每次比较次数-1,直到不需要比较

# include<iostream>
using namespace std;

int main()
{
	int arr[9] = { 4,2,8,0,5,7,1,3,9 };
	cout << "排序前:";
	for (int i = 0; i < 9; i++)
	{
		cout << arr[i] << ", ";
	}
	cout << endl;

	// 利用冒泡排序实现升序排列
	int temp;
	for (int i = 0; i < 9; i++) // 总排序轮数为元素个数-1
	{
		for (int j = i + 1; j < 9; j++)
		{
			if (arr[i] > arr[j])
			{
				temp = arr[i];
				arr[i] = arr[j];
				arr[j] = temp;
			}
		}
	}

	cout << "排序后:";
	for (int i = 0; i < 9; i++)
	{
		cout << arr[i] << ", ";
	}
	cout << endl;

	system("pause");
	return 0;
}

2.二维数组

在一维的基础上多加了一个维度(有行有列)

(1)定义

四种定义方式:

①数据类型 数组名[行数][列数];

# include<iostream>
using namespace std;

int main()
{
	int arr[2][3];

	arr[0][0] = 1;
	arr[0][1] = 2;
	arr[0][2] = 3;
	arr[1][0] = 4;
	arr[1][1] = 5;
	arr[1][2] = 6;

	// 利用循环进行输出:外层循环打印行数,内层循环打印列数
	for (int i = 0; i < 2; i++)
	{
		for (int j = 0; j < 3; j++)
		{
			cout << arr[i][j] << endl;
		}
	}

	system("pause");
	return 0;
}

②数据类型 数组名[行数][列数] = {{数据1,数据2},{数据3,数据4}……};

# include<iostream>
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;
}

③数据类型 数组名[行数][列数] = {数据1,数据2,数据3,数据4,……};

# include<iostream>
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;
}

④数据类型 数组名[ ][列数] = {数据1,数据2,数据3,数据4,……};

# include<iostream>
using namespace std;

int main()
{
	int arr[][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;
}

(2)数组名

查看二维数组所占内存空间

获取二维数组首地址

# include<iostream>
using namespace std;

int main()
{
	int arr[2][3] = { 1,2,3,4,5,6 };

	// 查看空间
	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;

	//查看地址
	cout << "二维数组的首地址:" << (int)arr << endl;
	cout << "二维数组第一行元素的地址:" << (int)arr[0] << endl;
	cout << "二维数组第一个元素的地址" << (int)&arr[0][0] << endl;
	cout << "二维数组第二个元素的地址" << (int)&arr[0][1] << endl;

	system("pause");
	return 0;
}

<案例>

考试成绩统计

三名同学(张三、李四、王五)的成绩如下:

语文数学英语
张三100100100
李四9050100
王五607080

分别输出三名同学的总成绩

# include<iostream>
using namespace std;
# include<string>

int main()
{
	// 存放名字
	string names[3] = { "张三","李四","王五" };

	// 存放分数
	int scores[3][3] =
	{
		{100,100,100},
		{90,50,100},
		{60,70,80}
	};

	// 计算并输出总分
	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;
}

07 函数

作用:将一段经常使用的代码封装起来,减少重复代码

一个较大的程序,可以分为若干个程序块,每个模块实现特定的功能

1.定义

五个步骤:

(1)返回值类型

(2)函数名

(3)参数列表

(4)函数体语句

(5)return表达式

语法:

返回值类型 函数名(参数列表)

{

        函数体语句;

        return表达式;

}

// 以下函数实现两个整形数据相加,并将相加的结果返回
int add(int num1, int num2) // 返回值类型为整型 参数列表为两个需要相加的数据
{
	int sum = num1 + num2; // 函数体实现两个数相加并将结果赋给sum
	return sum; // 通过return将相加的结果返回
}

2.调用

即使用定义好的函数

语法:函数名(参数)

# include<iostream>
using namespace std;

// 定义函数
int add(int num1, int num2) // 函数定义时的参数(num1和num2)本身并没有值,为形参(形式参数)
{
	int sum = num1 + num2;
	return sum;
}

int main()
{
	int a = 10;
	int b = 20; // a和b为实参(实际参数)

	int c = add(a, b); // 函数调用时会将相应的实参传递给形参
	cout << "c = " << c << endl;

	system("pause");
	return 0;
}

3.值传递

函数调用时将实参的值传递给形参

值传递时,形参的改变不会影响实参

# include<iostream>
using namespace std;

// 通过函数实现两个数值的交换
//void为空类型,不需要返回值
void swap(int num1, int num2)
{
	cout << "交换前:" << num1 << ", " << num2 << endl;

	int temp;
	temp = num1;
	num1 = num2;
	num2 = temp;

	cout << "交换后:" << num1 << ", " << num2 << endl;
}

int main()
{
	int a = 10;
	int b = 20;

	swap(a, b);

	system("pause");
	return 0;
}

关于“值传递时,形参的改变不会影响实参”的个人理解:

函数定义时num1和num2为形式参数,用以接收实参传递来的值,本身并没有值

实际参数为a = 10,b = 20,为实际参数

在函数调用时,将实际参数的值传递给形参,可以简单理解为:

num1 = a = 10;

num2 = b =20;

num1和num2得到的只是a和b的值,在函数调用结束后会释放,所以不会影响到a和b

4.常见函数样式

(1)无参无返

# include<iostream>
using namespace std;

// 定义
void test01()
{
	cout << "this is test01" << endl;
}

int main()
{
	//调用
	test01();

	system("pause");
	return 0;
}

(2)有参无返

# include<iostream>
using namespace std;

void test02(int a)
{
	cout << "this is test02: a = " << a << endl;
}

int main()
{
	int a = 100;
	test02(a);

	system("pause");
	return 0;
}

(3)无参有返

# include<iostream>
using namespace std;

int test03()
{
	cout << "this is test03" << endl;
	return 1000;
}

int main()
{
	int num1 = test03();
	cout << "num1 = " << num1 << endl;

	system("pause");
	return 0;
}

(4)有参有返

# include<iostream>
using namespace std;

int test04(int a)
{
	cout << "this is test04: a = " << a << endl;
	return a;
}

int main()
{
	int num2 = test04(10000);
	cout << "num2 = " << num2 << endl;

	system("pause");
	return 0;
}

5.声明

告诉编译器函数的名称,以及如何调用,函数的实际主体可以单独定义

注:函数的声明可以多次,但函数的定义只能有一次

# include<iostream>
using namespace std;

//声明
int max(int, int); // 包括 返回值类型 函数名 参数列表 分号

int main()
{
	int a = 10;
	int b = 20;

	// 调用
	int c = max(a, b);
	cout << "c = " << c << endl;

	system("pause");
	return 0;
}
// 定义
int max(int a, int b)
{
	return a > b ? a : b;
}

6.分文件编写

作用:让代码结构更加清晰

四个步骤:

(1)创建后缀名为.h的头文件

(2)创建后缀名为.cpp的源文件

(3)在头文件中写函数的声明

# include<iostream>
using namespace std;

// 头文件中写函数声明
void swap(int a, int b);

(4)在源文件中写函数的定义

# include<iostream>
using namespace std;

// 源文件中写函数定义
void swap(int a, int b)
{
	int temp = a;
	a = b;
	b = temp;

	cout << "a = " << a << endl;
	cout << "b = " << b << endl;
}

# include<iostream>
using namespace std;
# include"swap.h"

int main()
{
	int a = 10;
	int b = 20;

	swap(a, b);

	system("pause");
	return 0;
}

08 指针

内存编号从0开始记录,一般用十六进制数字表示

可以用指针变量保存地址

可以通过指针来保存一个地址,实现间接访问内存

1.定义和使用

定义语法:数据类型 *变量名;

# include<iostream>
using namespace std;

int main()
{
	int a = 10;

	// 定义
	int* p; // 定义指针p
	p = &a; // 指针p指向变量a的地址

	cout << "变量a的地址:" << &a << endl;
	cout << "指针p:" << p << endl;

	// 使用
	*p = 1000;

	cout << "*p = " << *p << endl;
	cout << "a = " << a << endl;


	system("pause");
	return 0;
}

2.指针所占内存空间

# include<iostream>
using namespace std;

int main()
{
	int a = 10;
	int* p = &a;

	cout << "sizeof(int*) = " << sizeof(int*) << endl;
	cout << "sizeof(float*) = " << sizeof(float*) << endl;
	cout << "sizeof(double*) = " << sizeof(double*) << endl;
	cout << "sizeof(char*) = " << sizeof(char*) << endl;

	system("pause");
	return 0;
}

在32位操作系统下,指针占4个字节空间大小,不管是什么数据类型

在64位操作系统下,指针占8个字节空间大小,不管是什么数据类型

3.空指针和野指针

(1)空指针

指针变量指向内存中编号为0的空间

用途:初始化指针变量

注:空指针指向的内存是不可以访问的

(0~255之间的内存编号是系统占用的,因此不可以访问)

# include<iostream>
using namespace std;

int main()
{
	int* p = NULL;

	system("pause");
	return 0;
}

(2)野指针

指针变量指向非法的内存空间,在程序中应尽量避免

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值