• C++---基础语法入门


    基础语法入门

    1.C++初识

    1.1C++初识的基本结构

    #include 
    using namespace std;
    
    int main()
    {
    	cout << "Hello World" << endl;
    	system("pause");
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    1.2注释

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

    1.2.1注释的两种格式

    1.2.1.1单行注释
    • 通常放在一行代码的上方,或者一条语句的末尾,对该行代码说明
    // 描述信息
    
    • 1
    1.2.1.2多行注释
    • 通常放在一段代码的上方
    /* 描述信息 */
    
    • 1
    1.2.1.3演示
    #include 
    using namespace std;
    /*
    	main是程序的一个入口,每个程序都必须有这么一个函数,有且只有一个
    */
    int main()
    {
    	// 在屏幕中输出hello world
    	cout << "hello world" << endl;
    	system("pause");
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    1.3变量

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

    1.3.1语法

    数据类型 变量名 = 初始值;
    
    • 1

    1.3.2演示

    #include
    using namespace std;
    
    int main()
    {
    	int num = 10;
    	cout << "num= " << num << endl;
    	system("pause");
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    1.4常量

    • 用于记录程序中不可更改的数据

    1.4.1定义常量的两种方式

    1.4.1.1 #define宏常量
    • 通常在文件上方定义,表示常量
    #define 常量名 常量值
    
    • 1
    1.4.1.2 const修饰的常量
    • 通常在变量定义前面加关键字const,修饰该变量为常量,不可修改
    const 数据类型 常量名 = 常量值
    
    • 1

    1.4.2常量演示

    #include
    using namespace std;
    #define Day 7
    
    int main()
    {
    	const int MONTH = 12;
    	cout << "一周总共有多少天" << Day << "天" << endl;
    	cout << "一年有多少月份" << MONTH << "个" << endl;
    	system("pause");
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    1.5关键字

    • 关键字是C++中预留保留的单词
    • 在定义变量常量时候,不要用关键字

    1.5.1不常见的关键字

    asmtypedefautoinlinetypeidbool
    dynamic_castsignedtypenamemutableunioncatch
    explictstatic_castvirtualoperatorwchar_tfriend
    register

    1.5.2演示

    #include
    using namespace std;
    int main()
    {
    	// 关键字不能给变量或常量命名
    	// int typeid = 12; // error,关键字不能作为变量名称
    	// const string typename = "法外狂徒张三"; // error,关键字不能作为常量名称
    	system("pause");
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    1.6标识符命名规则

    1.6.1规则

    • C++规定给标识符(变量、常量)命名时,有一套自己的规则
      • 标识符不能是关键字
      • 标识符只能由字母数字下划线组成
      • 第一个字符必须为字母下划线
      • 标识符种字母区分大小写

    1.6.2演示

    #include
    using namespace std;
    int main()
    {
    	// int int = 10; // 关键字不能作为变量名
    	// int 2df = 12; // 变量不能用数字开头,只能使用下划线或字母
    	int _2df = 16; // 可以使用下划线开头
    	int fde = 15; 
    	// cout << FDE << endl; // 标识符严格区分大小写
    	system("pause");
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    2.数据类型

    • C++规定在创建一个变量或常量时,必须要指定出相应的数据类型,否则无法给变量分配内存

    2.1整型

    • 整型变量表示的是整数类型的数据
    • C++中能够表示整型的类型有以下几种方式,区别在于所占内存空间不同
    数据类型占用空间取值范围
    short(短整型)2字节-2^15 ~ 2^15 - 1
    int(整型)4字节-2^31 ~ 2^31-1
    long(长整型)windows为4字节,Linux为4字节(32为),8字节(64位)-2^31 ~ 2^31 - 1
    long long(长长整型)8字节-2^63 ~ 2^63-1

    2.1.2演示

    #include
    using namespace std;
    int main() 
    {
    	// 短整型
    	short num = 20;
    	// 整型
    	int num1 = 20;
    	// 长整型
    	long num2 = 20;
    	// 长长整型
    	long long num3 = 20;
    	system("pause");
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    2.2sizeof关键字

    • 利用sizeof关键字可以统计数据类型所占内存大小

    2.2.1语法

    sizeof(数据类型 / 变量)
    
    • 1

    2.2.2演示

    #include
    using namespace std;
    int main()
    {
    	short num1 = 10;
    	cout << "short占用的内存空间" << sizeof(num1) << endl;
    	system("pause");
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    2.3实型(浮点型)

    • 用于表示小数

    2.3.1浮点型变量分为两种

    • 单精度float
    • 双精度double
    • 区别: 表示的有效数字范围不同
    • 默认情况下,会默认显示六位有效数字
    数据类型占用空间有效数字范围
    float4字节7位有效数字
    double8字节15~16位有效数字

    2.3.2演示

    #include
    using namespace std;
    int main()
    {
    	// 单精度
    	float f1 = 3.14f;
    	// 双精度
    	double d1 = 3.14;
    	// 科学计数法
    	double d2 = 3.2e3;
    	system("pause");
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    2.4字符型

    • 字符型变量用于显示单个字符
    • C和C++中字符型变量只占用1个字节
    • 字符型变量并不是把字符本身放到内存中存储,而是将对应的ASCII编码放到存储单元

    2.4.1语法

    • 在现实字符型变量时,用单引号括起来,不要用双引号
    • 单引号只能有一个字符,不可以是字符串
    char ch = 'a';
    
    • 1

    2.4.2演示

    #include
    using namespace std;
    int main()
    {
    	char sh = 'a';
    	cout << "字符型变量所占的内存" << sizeof(sh) << endl;
    
    	// char ch = "e" // error,创建字符型变量,只能使用单引号
    	system("pause");
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    2.5 转义字符

    • 用于表示一些不能显示出来的ASCII字符
    • 常见的转义字符表
    转义字符含义ASCII(十进制)
    \a警报007
    \b退格(BS),将当前位置移到前一列008
    \f换页(FF),将当前位置移到下页开头012
    \n换行(LF),将当前位置移到下一行开头010
    \r回车(CR),将当前位置移到本行开头013
    \t水平制表(HT),(跳到下一个TAB位置)009
    \v垂直制表(VT)011
    \\代表一个反斜线字符"\"092
    代表一个单引号039
    "代表一个双引号034
    ?代表一个问好063

    2.5.1演示

    #include
    using namespace std;
    int main()
    {
    	// 换行符 \n
    	cout << "Hello World \n" << endl;
    	// 反斜杠
    	cout << "\\" << endl;
    	// 水平制表符
    	cout << "Hello \t" << endl;
    	system("pause");
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    2.6字符串型

    • 用于表示一串字符

    2.6.1字符串的两种形式

    2.6.1.1C风格字符串
    • 语法
    char 变量名[] = 字符串量
    
    • 1
    2.6.1.2C++风格字符串
    • 语法
    string 变量名 = 字符串量
    
    • 1
    • 注意点: 使用string时,需要写一个头文件 #include

    2.6.2演示

    #include
    using namespace std;
    #include
    
    int main()
    {
    	// C风格字符串
    	char str[] = "法外狂徒张三";
    	cout << str << endl;
    	// c++风格字符串
    	// 包含一个头文件  #include
    	string strs = "法外狂徒张三";
    	cout << strs << endl;
    	system("pause");
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    2.7布尔类型

    • 布尔数据类型代表/的值
    • bool类型只有两个值
      • true — (本质是1)
      • false — (本质是0)
    • bool类型1个字节大小

    2.7.1演示

    #include
    using namespace std;
    int main()
    {
    	bool flag = true;
    	cout << flag << endl; // 1
    	bool flag1 = false;
    	cout << flag1 << endl; // 0
    	cout << "布尔类型占用空间大小:" << sizeof(flag) << endl; // 布尔类型占用空间大小:1
    	system("pause");
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    2.8数据输入

    • 用于从键盘获取数据
    • 关键字 cin

    2.8.1语法

    cin >> 变量
    
    • 1

    2.8.2演示

    #include
    using namespace std;
    
    int main() 
    {
    	int num = 10;
    	// 键盘输入
    	cin >> num;
    	cout << "你输入的数字是:" << num << endl;
    	system("pause");
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    3.运算符

    • 用于执行代码的运算
    运算符类型作用
    算术运算符用于处理四则运算
    赋值运算符用于将表达式的值赋给变量
    比较运算符用于表达式的比较,并返回一个bool值
    逻辑运算符用于根据表达式的值返回bool值

    3.1算术运算符

    • 用于处理四则运算
    • 算术运算符包括以下符号
    运算符术语示例结果
    +正号+33
    -负号-3-3
    +10+515
    -10-55
    *10*550
    /10/52
    %取模10 % 50
    ++前置递增a = 2;b = ++aa = 3;b = 3
    ++后置递增a = 2;b = a++a = 3;b = 2
    前置递减a = 2; b = --aa = 1; b = 1
    后置递减a = 2,b = a–a = 1; b = 2

    3.1.1演示

    #include
    using namespace std;
    int main()
    {
    	// 自增运算
    	int num = 5;
    	int num1 = num++;
    	cout << "num = " << num << ";num1 = " << num1 << endl; // num = 6 ; num1 = 5
    	// 除法   两个整数相除,值为整数
    	cout << "num / num1 = " << num / num1 << endl; // 1
    	system("pause");
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    3.2赋值运算符

    • 用于将表达式的值赋给变量
    • 赋值运算符包括以下几个符号
    运算符术语示例结果
    =赋值a = 2
    +=加等a = 0;a += 2a = 2
    -=减等a = 5;a -= 2a = 3
    *=乘等a = 5; a *= 2a = 10
    /=除等a = 5; a /= 2a = 2
    %=模等a = 5; a %= 2a = 1

    3.2.1演示

    #include
    using namespace std;
    
    int main()
    {
    	int num = 5;
    	int num1 = 2;
    	cout << "(num *= num1) = " << (num *= num1) << endl; // 10
    	cout << "(num %= num1) = " << (num %= num1) << endl; // 0
    	system("pause");
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    3.3比较运算符

    • 用于比较表达式的比较,并返回一个bool值
    • 比较运算符包含以下符号
    运算符术语示例结果
    ==相等于4 == 30
    !=不等于4 != 31
    <小于4 < 30
    >大于4 > 31
    <=小于等于4 <= 30
    >=大于等于4 >= 31

    3.3.1演示

    #include
    using namespace std;
    
    int main()
    {
    	int num1 = 8;
    	int num2 = 10;
    	cout << (num1 <= num2) << endl; // 1
    	cout << (num1 != num2) << endl; // 1
    	cout << (num1 > num2) << endl; // 0
    	system("pause");
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    3.4逻辑运算符

    • 用于根据表达式的值返回bool值
    • 逻辑运算符有以下符号
    运算符术语示例结果
    !!a如果a为假,则!a为真
    &&a && b如果a和b都为真,则为真
    ||a || b若a和b都为假,则为假

    3.4.1演示

       #include
       using namespace std;
       
       int main()
       {
       	int num = 10;
       	cout << !num << endl; // 0
       	bool flag = true;
       	cout << (num && flag) << endl; // 1
       	cout << (num || flag) << endl; // 1
       	system("pause");
       	return 0;
       }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    4.程序流程结构

    • C/C++支持最基本的三种程序运行结构:顺序结构选择结构循环结构
      • 顺序结构:程序按顺序执行,不发生跳转
      • 选择结构:依据条件是否满足,有选择的执行相应功能
      • 循环结构:依据条件是否满足,循环多次执行某段代码

    4.1选择结构

    4.1.1if语句

    • 执行满足条件的语句
    4.1.1.1if语句有三种形式
    4.1.1.1.1单行格式if语句
    • 语法
    if(条件){条件满足执行的语句}
    
    • 1
    • 演示
    #include
    using namespace std;
    
    int main()
    {
    
    	int score = 0;
    	cout << "请输入你的高考分数:" << endl;
    	cin >> score;
    	if (score > 650) {
    		cout << "恭喜你考上了211大学" << endl;
    	}
    	system("pause");
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    4.1.1.1.2多行格式if语句
    • 语法
    if(条件){满足条件执行}else{不满足条件执行}
    
    • 1
    • 演示
    #include
    using namespace std;
    int main()
    {
    	int score = 0;
    	cout << "请输入你的成绩:" << endl;
    	cin >> score;
    	if (score > 620) 
    	{
    		cout << "恭喜你被我校录取" << endl;
    	}
    	else {
    		cout << "很抱歉,你未被我校录取" << endl;
    	}
    	system("pause");
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    4.1.1.1.3多条件的if语句
    • 语法
    if(条件1){满足条件1执行}else if(条件2){满足条件2执行}....else{未满足以上全部条件执行}
    
    • 1
    • 演示
    #include
    using namespace std;
    
    int main()
    
    {
    	int score = 0;
    	cout << "请输入你的分数" << endl;
    	cin >> score;
    	cout << "你输入的成绩为:" << score << endl;
    	if (score > 650) 
    	{
    		cout << "你是我省高考成绩的前5%" << endl;
    	}else if(score > 600){
    		cout << "你是我省高考成绩的前10%" << endl;
    	}
    	else {
    		cout << "你的成绩未纳入统计" << endl;
    	}
    	system("pause");
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    4.1.1.1.4嵌套if语句
    • 语法
    if(条件1){if(条件1_1){满足条件1_1执行语句}else{未满足条件1_1执行语句}}else{未满足条件1执行执行语句}
    
    • 1

    4.1.2三目运算

    • 通过三目运算实现简单的判断
    • 语法
    表达式1? 表达式2 : 表达式3
    
    • 1
    4.1.2.1演示
    #include
    using namespace std;
    
    int main()
    {
    	double pig1 = 0;
    	double pig2 = 0;
    	double pig3 = 0;
    	cout << "请输入一号猪的体重:" << endl;
    	cin >> pig1;
    	cout << "请输入二号猪的体重:" << endl;
    	cin >> pig2;
    	cout << "请输入三号猪的体重:" << endl;
    	cin >> pig3;
    
    	pig1 < pig2 ? (pig2 < pig3 ? (cout << "最重的小猪pig3:" << pig3 << endl) : (cout << "最重的小猪pig2:" << pig2 << endl)) : (pig1 < pig3?(cout << "最重的小猪pig3:" << pig3 << endl):(cout << "最重的小猪pig1:" << pig1 << endl));
    	system("pause");
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    4.1.3switch语句

    • 执行多条件分支语句
    • 语法
    switch(表达式)
    {
    	case 结果1: 执行语句;break;
    	case 结果2: 执行语句;break;
    	...
    	default: 执行语句;break;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    4.1.3.1演示
    #include
    using namespace std;
    
    int main()
    
    {
    	int num = 0;
    	cout << "请输入需要执行的代码:" << endl;
    	cin >> num;
    
    	switch (num) {
    		case 1: cout << "请输入密码:" << endl;
    			break;
    		case 2: cout << "请输入账号:" << endl;
    			break;
    		case 3: cout << "请输入类型:" << endl;
    			break;
    		case 0: cout << "转接人工!" << endl;
    			break;
    		default: cout << "重听一次" << endl;
    	}
    
    	system("pause");
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    4.2循环结构

    4.2.1while循环语句

    • 满足循环条件,执行循环语句
    • 语法
    while(循环条件){ 循环语句 }
    
    • 1
    4.2.1.1演示
    #include
    using namespace std;
    
    int main()
    {
    	int num = 0;
    	cout << "请输入你需要打印的次数:" << endl;
    	cin >> num;
    	while (num > 0) {
    		cout << "打印剩余" << num << "次数" << endl;
    		num--;
    	}
    	system("pause");
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    4.2.2do…while循环语句

    • 满足循环条件,执行循环语句
    • 语法
    do{循环语句} while(循环条件)
    
    • 1
    • 注意点: 与while的区别在于do…while会先执行一次循环语句,在判断循环条件
    4.2.2.1演示
    #include
    using namespace std;
    
    int main()
    
    {
    	int num = 0;
    	cout << "请输入你想循环的次数" << endl;
    	cin >> num;
    	do {
    		cout << "do...while语句会先执行一次循环语句" << endl;
    		num--;
    	} while (num > 0);
    	system("pause");
    	return 0;
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    4.2.3for循环语句

    • 满足循环条件,执行循环语句
    • 语法
    for(起始表达式;条件表达式;末尾循环体) {循环语句;}
    
    • 1
    4.2.3.1演示
    #include
    using namespace std;
    
    int main()
    {
    	for (int num = 10; num > 0; num--) {
    		cout << "循环的剩余" << num << "次数" << endl;
    	}
    	system("pause");
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    4.2.4嵌套循环

    • 在循环中嵌套一层循环,解决一些实际问题
    4.2.4.1演示
    #include
    using namespace std;
    
    int main()
    {
    	int num = 0;
    	cout << "请输入你想打印的矩阵图像" << endl;
    	cin >> num;
    	while (num > 0) {
    		for (int i = 10; i > 0; i--) {
    			cout << "*\t";
    		}
    		cout << endl;
    		num--;
    	}
    	system("pause");
    	return 0;
    	
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    4.3跳转语句

    4.3.1break语句

    • 用于跳出选择结构或循环结构
    4.3.1.1break使用场景
    • 出现在switch条件语句中,作用是终止case并跳出switch
    • 出现在循环语句中,作用是跳出当前循环语句
    • 出现在嵌套循环中,跳出最近的内层循环语句
    4.3.1.2演示
    #include
    using namespace std;
    
    int main()
    {
    	// break使用场景
    
    	// 1.出现在switch语句中
    	cout << "请选择副本难度" << endl;
    	cout << "1、普通" << endl;
    	cout << "2、中等" << endl;
    	cout << "3、困难" << endl;
    	cout << "4、DLC" << endl;
    
    	int selType = 0;
    	cin >> selType;
    	switch (selType) {
    		case 1:
    			cout << "你选择的游戏难度为普通" << endl;
    			break;
    		case 2:
    			cout << "你选择的游戏难度为中等" << endl;
    			break;
    		case 3:
    			cout << "你选择的游戏难度为困难" << endl;
    			break;
    		case 4:
    			cout << "你选择的游戏难度为DLC" << endl;
    			break;
    		default:
    			cout << "你选择的游戏难度无效" << endl;
    	}
    
    	// 2.循环语句
    	int num = 0;
    	cout << "请输入你要循环的次数" << endl;
    	cin >> num;
    	while (num > 0) {
    		if (num == 5) {
    			cout << "循环次数无效,即将退出" << endl;
    			break;
    		}
    		cout << "循环次数有效" << endl;
    		num--;
    	}
    
    
    	// 嵌套语句
    	int nums = 0;
    	cout << "请输入你要执行的次数,缺偶数位图标" << endl;
    	cin >> nums;
    	while (nums > 0) {
    		for (int i = 10; i > 0; i--) {
    			cout << "* ";
    			if (i < 4) {
    				break;
    			}
    		}
    		cout << endl;
    		nums--;
    	}
    	system("pause");
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64

    4.3.2continue语句

    • 在循环语句中,跳过本次循环中余下的尚未执行的语句,继续执行下一次循环
    4.3.2.1演示
    #include
    using namespace std;
    
    int main()
    {
    
    	for (int i = 100; i > 0; i--) {
    		if (i % 2 == 0) {
    			continue;
    		}
    		cout << i << endl; // 99 97 95 93 91 .....
    	}
    
    	system("pause");
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    4.3.3goto语句

    • 可以无条件跳转语句
    • 语法
    goto 标记;
    
    • 1
    4.3.3.1演示
    #include
    using namespace std;
    
    int main()
    {
    	cout << "1、****" << endl;
    	cout << "2、****" << endl;
    	goto FLAG;
    	cout << "3、****" << endl;
    	cout << "4、****" << endl;
    	cout << "5、****" << endl;
    	FLAG:
    	cout << "6、****" << endl;
    	cout << "7、****" << endl;
    	system("pause");
    	return 0;
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    5.数组

    5.1概述

    • 所谓数组,就是一个集合,里面存在了相同类型的数据元素
    • 数组中的每个数据元素都是相同的数据类型
    • 数组是由连续的内存位置组成的

    5.2一维数组

    5.2.1一维数组定义方式

    • 一维数组定义的三种定义
      • 数据类型 数组名[数组长度]
      • 数据类型 数组名[数组长度] = {值1,值2,值3,…}
      • 数据类型 数组名[ ] = {值1,值2,…}
    5.2.1.1演示
    #include
    using namespace std;
    
    int main()
    {
    	// 1.数据类型 数组名[数组长度]
    	int arr[10];
    	// 给数组赋值
    	arr[0] = 10;
    	arr[1] = 20;
    	cout << arr[1] << endl; // 20
    
    	//2. 数据类型 数组名[数组长度] = {值1,值2,...}
    	int arr2[5] = { 10,20,30,40,50 };
    	cout << arr2[2] << endl; // 30
    
    	//3. 数据类型 数组名[] = {值1,值2,值3,...}
    	int arr3[] = { 10,20,30,40 };
    	cout << arr3[2] << endl; // 30
    	system("pause");
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    5.2.2一维数组数组名

    • 一维数组名称的用途
      • 可以统计整个数组在内存中的长度
      • 可以获取数组在内存中的首地址
    • 注意点: 数组名是一个常量,不能修改
    5.2.2.1演示
    #include
    using namespace std;
    
    int main() 
    {
    	int arr[5] = { 10,20,30,40,50 };
    	cout << "获取数组在内存中的大小:" << sizeof(arr) << endl; // 20
    	cout << "获取数组中每个元素在内存中的大小:" << sizeof(arr[0]) << endl; // 4
    	cout << "获取数组的长度:" << sizeof(arr) / sizeof(arr[0]) << endl; // 5
    	cout << "数组首地址为:" << arr << endl;
    	cout << "数组中第一个元素地址为:" << &arr[0] << endl;	
    	system("pause");
    	return 0 ;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    5.2.3冒泡排序

    • 最常用的排序算法,对数组内元素进行排序
    5.2.3.1演示
    #include
    using namespace std;
     
    int main()
    {
    	int arr[] = { 12,10,6,20,15,30,45,20 };
    	for (int i = 0; i < sizeof(arr) / sizeof(arr[0]) - 1; i++) 
    	{
    		for (int j = 0; j < sizeof(arr) / sizeof(arr[0]) - 1 - i; j++)
    		{
    			if (arr[j] > arr[j+1])
    			{
    				int temp = arr[j+1];
    				arr[j + 1] = arr[j];
    				arr[j] = temp;
    			}
    		}
    	
    	}
    	for (int len = 0; len < sizeof(arr) / sizeof(arr[0]); len++) {
    		cout << arr[len] << "\t";
    	}
    
    	cout << endl;
    	system("pause");
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    5.3二维数组

    • 二维数组就是在一维数组上,多加一个维度

    5.3.1二维数组定义方式

    • 二维数组定义的四种方式
      • 数据类型 数组名[行数][列数]
      • 数据类型 数组名[行数][列数] = {{数据1,数据2,数据3,…},{数据4,数据5,数据6,…},{数据7,数据8,数据9,…},…}
      • 数据类型 数组名[行数][列数] = {数据1,数据2,数据3,…}
      • 数据类型 数组名[][列数] = {数据1,数据2,数据3,…}
    5.3.1.1演示
    #include
    using namespace std;
    
    int main()
    {
    	//1.数据类型 数组名[行数][列数]
    	int arr1[2][3];
    	arr1[0][0] = 12;
    	arr1[0][1] = 16;
    	arr1[0][2] = 15;
    	cout << arr1[0][1] << endl; // 16
    
    	//2.数据类型 数组名[行数][列数] = {{数据1,数据2,...},{数据3,数据4,...},...}
    	int arr2[3][3] = { {2,8,6},{5,9,4},{1,101,15} };
    	cout << arr2[1][2] << endl; // 4
    
    	//3.数据类型 数组名[行数][列数] = {数据1,数据2,数据3,...}
    	int arr3[2][3] = { 12,15,16,18 };
    	cout << arr3[1][0] << endl; // 18
    
    	//4.数据类型 数组名[][列数] = {数据1,数据2,数据3,...}
    	int arr4[][3] = { 12,16,14 };
    	cout << arr4[0][1] << endl; // 16
    
    	system("pause");
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    5.3.2二位数组数组名

    • 查看二维数组所占内存空间
    • 获取二位数组首地址
    5.3.2.1演示
    #include
    using namespace std;
    
    
    int main()
    {
    	int arr[2][2] = { {13,21},{16,19} };
    	// 查看占用空间的大小
    	cout << "二维数组占用空间大小:" << sizeof(arr) << endl; // 16
    	cout << "二位数组第一行占用空间大小:" << sizeof(arr[0]) << endl; // 8
    	cout << "二位数组第一个元素占用空间为:" << sizeof(arr[0][0]) << endl; // 4
    	cout << "二维数组的行数为:" << sizeof(arr) / sizeof(arr[0]) << endl;// 2
    	cout << "二维数组的列数为:" << sizeof(arr[0]) / sizeof(arr[0][0]) << endl; // 2
    
    	// 可以查看二维数组的首地址  可以使用(int)强转
    	cout << "二位数组的首地址:" << arr << endl;
    	cout << "二维数组的第一个元素首地址:" << &arr[0][0] << endl;
    	system("pause");
    	return 0;	
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    6.函数

    6.1概述

    • 将一段经常使用的代码封装起来,减少重复代码
    • 一个较大的程序,一般分为若干个程序块,每个模块实现特定的功能

    6.2函数的定义

    6.2.1实现函数的5个步骤

    • 函数的定义一般主要有5个步骤
      • 返回值类型
        • 如果函数没有返回值,可以使用void
      • 函数名
      • 参数列表
      • 函数体语句
      • return 表达式
        • 如果函数没有返回值return可以不写return;

    6.2.2函数的语法

    返回值类型 函数名 (参数列表)
    {
    	函数体语句;
    	return 表达式;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    6.2.3函数演示

    #include
    using namespace std;
    
    int add(int num1, int num2)
    {
    	int sum = num1 + num2;
    	return sum;
    }
    
    int main()
    {
    	int sum = add(10, 20); // 30
    	cout << sum << endl;
    	system("pause");
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    6.3函数的调用

    • 使用定好的函数

    6.3.1函数调用的语法

    函数名(参数)
    
    • 1

    6.3.2函数调用的演示

    #include
    using namespace std;
    
    // 方法括号里面的称为形参
    double decl(double d1, double d2)
    {
    	double result = d1 - d2;
    	return result;
    }
    
    int main()
    {
    	// 实参
    	double d1 = 12.45;
    	double d2 = 5.689;
    
    	// 函数调用
    	double result = decl(d1, d2);
    
    	cout << "计算的结果为:" << result << endl;
    	system("pause");
    	return 0;	
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    6.4值传递

    • 所谓值传递,就是函数调用时实参将数值传入给形参
    • 值传递时,如果形参发生变化并不会影响实参

    6.4.1值传递演示

    #include
    using namespace std;
    
    void swap(int num1, int num2)
    {
    	int temp = num1;
    	num1 = num2;
    	num2 = temp;
    	cout << "num1 = " << num1 << endl;
    	cout << "num2 = " << num2 << endl;
    	// return;  // 函数没有返回值的时候,可以不用写或return;
    }
    
    int main()
    {
    	int num1 = 100;
    	int num2 = 98;
    	// 当做值传递的时候,函数的形参发生改变,并不会影响实参
    	swap(num1, num2);
    	system("pause");
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    6.5函数常见样式

    6.5.1常见函数的4中样式

    • 无参无返
    // 无参无返
    void noParamter()
    {
    	int num = 10;
    	cout << "函数样式之一  无参无返" << endl;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 有参无返
    // 有参无返
    void noReturn(int num)
    {
    	cout << "输入的参数为:" << num << endl; // 18
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 无参有返
    // 无参有返
    int hasReturn() 
    {
    	int num = 15;
    	int sum = num * num;
    	return sum;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 有参有返
    // 有参有返
    int hasParamter(int num) 
    {
    	int result = num * 10;
    	return result;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    6.5.2函数常见样式的演示

    #include
    using namespace std;
    
    // 无参无返
    void noParamter()
    {
    	int num = 10;
    	cout << "函数样式之一  无参无返" << endl;
    }
    
    // 有参无返
    void noReturn(int num)
    {
    	cout << "输入的参数为:" << num << endl; // 18
    }
    
    
    // 无参有返
    int hasReturn() 
    {
    	int num = 15;
    	int sum = num * num;
    	return sum;
    }
    
    // 有参有返
    int hasParamter(int num) 
    {
    	int result = num * 10;
    	return result;
    }
    
    int main()
    {
    	int num = 18;
    	noParamter();
    	noReturn(num);
    	int sum = hasReturn();
    	cout << "无参有返的计算结果:" << sum << endl; // 225
    	int result = hasParamter(num);
    	cout << "有参有返的计算结果为: " << result << endl; //180
    	system("pause");
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44

    6.6函数的声明

    • 告诉编译器函数名称及如何调用函数,函数的实际主体可以单独定义
    • 函数的声明可以多次,但是函数的定义只能有一次

    6.6.1函数声明的演示

    #include
    using namespace std;
    
    // 函数的声明   函数的定义可以在程序入口的后面
    int max(int a, int b);
    
    int main()
    {
    	int num1 = 88;
    	int num2 = 99;
    	cout << "取最大数:" << max(num1, num2) << endl;
    
    	system("pause");
    	return 0;
    }
    
    int max(int a, int b) 
    {
    	return a < b ? b : a;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    6.7函数的分文件编写

    • 让代码结构更加清晰

    6.7.1函数分文件编写的4个步骤

    • 创建后缀名为.h的头文件
    #include
    using namespace std;
    
    void swap(int a, int b);
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 创建后缀名为.cpp的源文件
    #include "函数的头文件.h"
    
    void swap(int num1, int num2)
    {
    	int tmep = num1;
    	num1 = num2;
    	num2 = tmep;
    	cout << "交换数据之后" << endl;
    	cout << "num1 = " << num1 << endl;
    	cout << "num2 = " << num2 << endl;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 在头文件中写函数的声明
    • 在源文件中写函数的定义
    #include
    using namespace std;
    #include "函数的头文件.h"
    
    int main()
    {
    	int a = 15;
    	int b = 19;
    	swap(a,b);
    	system("pause");
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    7.指针

    7.1指针的基本概念

    • 可以通过指针间接访问内存
    • 内存编号时从0开始记录的,一般用十六进制数字表示
    • 可以利用指针变量保存地址

    7.2指针变量的定义和使用

    • 指针变量定义语法
    数据类型 * 变量名
    
    • 1

    7.2.1指针变量的定义和使用的演示

    #include
    using namespace std;
    
    int main()
    {
    	// 1.定义指针
    	int num = 10;
    	//指针定义的语法: 数据类型 * 指针变量名
    	int* p;
    	//让指针记录变量a的地址
    	p = &num;
    	cout << "num的地址为:" << &num << endl;
    	cout << "指针p为: " << p << endl;
    
    	// 2.使用指针
    	// 可以通过解引用的方式来找到指针指向的内存
    	//指针前加 * 代表解引用,找到指针指向的内存中的数据
    	*p = 156;
    	cout << "num = " << num << endl;
    	cout << "*p = " << *p << endl;
    
    
    	system("pause");
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    7.3指针所占空间

    • 指针也是数据类型

    7.3.1指针所占空间的演示

    #include
    using namespace std;
    
    int main()
    {
    	int num = 10;
    	int* p;
    	p = &num;
    	// 在32位操作系统下,指针都是占4个字节;在64位操作系统下,指针都是占8个字节
    	cout << "指针的地址为:" << p << endl; // 
    	cout << "指针所占的内存大小:" << sizeof(p) << endl; // 4
    	cout << "指针所占的内存大小:" << sizeof(int *) << endl; // 4
    	cout << "指针所占的内存大小:" << sizeof(double *) << endl; // 4
    	cout << "指针所占的内存大小:" << sizeof(long *) << endl; // 4
    	cout << "指针所占的内存大小:" << sizeof(long long*) << endl; // 4
    	cout << "指针所占的内存大小:" << sizeof(short *) << endl; // 4
    
    	system("pause");
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    7.4空指针和野指针

    • 空指针:指针变量指向内存中编号为0的空间
    • 用途: 初始化指针变量
    • 注意: 空指针指向的内存是不可以访问的
    • 0~255之间的内存编号是系统占用的,因此不可以访问

    7.4.1空指针的演示

    #include
    using namespace std;
    
    int main() 
    {
    	// 空指针
    	// 1.空指针用于个指针变量进行初始化
    	int* p = NULL;
    
    	// 2.空指针是不可以进行访问的
    	// 0~255之间的内存编号是系统占用的,因此不可以访问
    	// *p = 100; // error,空指针异常
    
    	system("pause");
    	return 0;
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    7.4.2野指针的演示

    #include
    using namespace std;
    
    int main()
    {
    	// 野指针
    	// 在程序中,尽量避免出现野指针
    	int* p = (int *)0x12FA2;
    	// cout << *p << endl; //error,未申请空间,因此不要访问
    
    	system("pause");
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    7.5const修饰指针

    7.5.1const修饰指针有三种情况

    7.5.1.1const修饰指针 — 常量指针
    • 常量指针特点: 指针的指向可以修改,但是指针指向的值不可以改

      #include
      using namespace std;
      
      int main()
      {
      	int num = 20;
      	const int* p = &num;
      	cout << "当前指针的地址为:" << p << endl;
      	cout << "当前指针指向的内容:" << *p << endl;
      	cout << "num = " << num << endl;
      
      	// *p = 12; // 编译报错
      	int nums = 89;
      	p = &nums;
      	cout << "当前指针的地址为:" << p << endl;
      	cout << "当前指针指向的内容:" << *p << endl;
      	system("pause");
      	return 0;
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
    7.5.1.2const修饰常量 — 指针常量
    • 特点: 指针的指向不可以改,**指针指向的值可以改
      #include
      using namespace std;
      
      int main()
      {
      	int a = 150;
      	int* const p = &a;
      	cout << "当前指针的地址为:" << p << endl;
      	cout << "当前指针指向的内容:" << *p << endl; // 150
      	*p = 188;
      	cout << "当前指针指向的内容:" << *p << endl; // 188
      	
      	int b = 123;
      	// p = &b;  //指针的指向不能修改
      	cout << "当前指针的地址为:" << p << endl;
      	cout << "当前指针指向的内容:" << *p << endl; // 188
      
      	system("pause");
      	return 0;
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
    7.5.1.3const即修饰指针,又修饰常量
    • 指针的指向和指针指向的值都不可以改
      #include
      using namespace std;
      
      int main()
      {
      	int num = 123;
      	int nums = 126;
      
      	const int* const p = &num;
      	// p = &nums;  // error
      	// *p = 125;	// error
      
      	system("pause");
      	return 0;
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15

    7.6指针和数组

    • 利用指针访问数组中的元素

    7.6.1指针和数组演示

    #include
    using namespace std;
    
    int main()
    {
    	// 数组
    	int arr[] = { 15,16,79,23,16 };
    
    	//指针
    	int* p = arr;
    	cout << "当前指针指向的内容" << *p << endl; // 15
    	// 偏移4个字节
    	p++;
    	cout << "当前指针指向的内容" << *p << endl; // 16
    	cout << arr << endl;
    	system("pause");
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    7.7指针和函数

    • 利用指针作函数参数,可以修改实参的值

    7.7.1指针和函数的演示

    #include
    using namespace std;
    
    // 函数声明
    void swap(int* p1, int* p2);
    
    int main()
    {
    	int a = 10;
    	int b = 15;
    	cout << &a << endl;
    	cout << &b << endl;
    	swap(&a, &b);
    
    	cout << &a << endl; // 此处&a 与  交换之前&a一致
    	cout << "a = " << a << endl;
    	cout << "b = " << b << endl;
    	system("pause");
    	return 0;
    }
    
    void swap(int* p1, int* p2) 
    {
    	int temp = *p1;
    	*p1 = *p2;
    	*p2 = temp;
    	cout << "a = " << *p1 << endl;
    	cout << "b = " << *p2 << endl;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    7.8指针、数组、函数

    • 头文件
    #include
    using namespace std;
    void bubbleSort(int* arr, int len);
    
    • 1
    • 2
    • 3
    • 源文件
    #include "冒泡排序的头文件.h"
    void bubbleSort(int* arr, int len)
    {
    	int* p = arr;
    	cout << *p << endl;
    	for (int i = 0; i < len - 1; i++) {
    		for (int j = 0; j < len - i - 1; j++) {
    			if (arr[j] > arr[j+1]) {
    				int temp = arr[j];
    				arr[j] = arr[j+1];
    				arr[j+1] = temp;
    			}
    		}
    	}
    	cout << "排序后的数组: " << endl;
    	for (int k = 0; k < len; k++) {
    		cout << *p << " ";
    		p++;
    	}
    	cout << endl;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 执行源文件
    #include
    using namespace std;
    #include "冒泡排序的头文件.h"
    int main()
    {
    	int arr[] = { 15,32,68,96,12,16,30 };
    	int len = sizeof(arr) / sizeof(arr[0]);
    	bubbleSort(arr, len);
    	system("pause");
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    8.结构体

    8.1结构体基本概念

    • 结构体属于用户自定义的数据模型,允许用户存储不同的数据模型

    8.2结构体定义和使用

    8.2.1语法

    struct 结构体名 {结构体成员列表}
    
    • 1
    • 演示
    // 1.结构体定义
    	struct Student 
    	{
    		string name;
    		int age;
    		float score;
    	};
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    8.2.2结构体创建变量的方法有三种

    8.2.2.1方式一
    • 语法
    struct 结构体 变量名
    
    • 1
    • 演示
    // 2.1struct Student stu;
    	// 给stu2属性赋值,通过访问结构体变量中的属性
    	struct Student stu1;
    	stu1.name = "法外狂徒张三";
    	stu1.age = 20;
    	stu1.score = 89.8f;
    	cout << "姓名:" << stu1.name << ",年龄:" << stu1.age << ",成绩:" << stu1.score << endl;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    8.2.2.2方式二
    • 语法
    struct 结构体名 变量名 = {成员1,成员2,...}
    
    • 1
    • 演示
    //2.2struct Student stu2 = {};
    	struct Student stu2 = { "富甲一方田七",19,85.6f };
    	cout << "姓名:" << stu2.name << ",年龄:" << stu2.age << ",成绩:" << stu2.score << endl;
    
    • 1
    • 2
    • 3
    8.2.2.3方式三
    • 语法
    定义结构体时顺便创建变量
    
    • 1
    • 演示
    //在定义结构体时顺便创建结构体变量
    	struct Person {
    		string name;
    		int age;
    		float Hight;
    		float weigth;
    	} person;
    	person.age = 18;
    	person.name = "知识渊博朝九";
    	person.Hight = 1.86;
    	person.weigth = 75;
    	cout << "姓名:" << person.name << ",年龄:" << person.age << ",身高:" << person.Hight << ",体重:" << person.weigth << endl;
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    8.2.3结构体定义和使用的演示

    #include
    using namespace std;
    #include
    
    int main()
    {
    	// 1.结构体定义
    	struct Student 
    	{
    		string name;
    		int age;
    		float score;
    	};
    
    	// 2.创建变量
    	// 2.1struct Student stu;
    	// 给stu2属性赋值,通过访问结构体变量中的属性
    	struct Student stu1;
    	stu1.name = "法外狂徒张三";
    	stu1.age = 20;
    	stu1.score = 89.8f;
    	cout << "姓名:" << stu1.name << ",年龄:" << stu1.age << ",成绩:" << stu1.score << endl;
    
    	//2.2struct Student stu2 = {};
    	struct Student stu2 = { "富甲一方田七",19,85.6f };
    	cout << "姓名:" << stu2.name << ",年龄:" << stu2.age << ",成绩:" << stu2.score << endl;
    
    	//在定义结构体时顺便创建结构体变量
    	struct Person {
    		string name;
    		int age;
    		float Hight;
    		float weigth;
    	} person;
    	person.age = 18;
    	person.name = "知识渊博朝九";
    	person.Hight = 1.86;
    	person.weigth = 75;
    	cout << "姓名:" << person.name << ",年龄:" << person.age << ",身高:" << person.Hight << ",体重:" << person.weigth << endl;
    
    	system("pause");
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43

    8.3结构体数组

    • 将自定义的结构体放入到数组中方便维护

    8.3.1结构体数组语法

    struct 结构体名  数组名[元素个数] = {{},{},{},...}
    
    • 1

    8.3.2结构体数组演示

    #include
    using namespace std;
    
    struct student
    {
    	string name;
    	int age;
    	int score;
    };
    
    int main()
    {
    	//结构体数组
    	struct student arr[3] = {
    		{"张三",18,89},
    		{"李四",19,96},
    		{"王五",20,87}
    	};
    	cout << sizeof(arr) << endl;// 108
    	cout << sizeof(arr[0].name) << endl; //28
    	arr[1].name = "田七";
    	for (int i = 0; i < 3; i++) {
    		cout << "姓名:" << arr[i].name << ",年龄:" << arr[i].age << ",成绩:" << arr[i].score << endl;
    	}
    	system("pause");
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    8.4结构体指针

    • 通过指针访问结构体中的成员
    • 利用操作符 -> 可以通过结构体指针访问结构体属性
    8.4.1结构体指针的演示
    #include
    using namespace std;
    #include
    
    //结构体
    struct student 
    {
    	string name;
    	int age;
    	char sex;
    };
    
    int main()
    {
    	// 创建学生结构体变量
    	struct student stu = { "法外狂徒张三",19,'1' };
    	// 通过指针指向结构体变量
    	struct student* p = &stu;
    	// 通过指针访问结构体变量中的数据
    	cout << "姓名: " << p->name << ",年龄: " << p->age << ",性别: " << p->sex << endl;
    
    	system("pause");
    	return 0;
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    8.5结构体嵌套结构体

    • 结构体中的成员可以是另一个结构体

    8.5.1结构体嵌套结构体演示

    #include
    using namespace std;
    #include
    
    // 结构体
    struct student 
    {
    	string name;
    	int age;
    	double score;
    };
    
    // 嵌套结构体
    
    struct teacher
    {
    	int id;
    	string name;
    	int age;
    	struct student stu;
    };
    
    
    int main()
    {
    	struct student stu = { "法外狂徒张三",19,89 };
    	struct teacher tea = { 1023,"罗翔",38,stu };
    	cout << "老师的编号: " << tea.name << ",老师带的学生的姓名: " << tea.stu.name << endl;
    	system("pause");
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31

    8.6结构体做函数参数

    • 将结构体作为参数向函数中传递

    8.6.1传递方式有两种

    • 值传递
    #include
    using namespace std;
    
    // 结构体
    struct student {
    	string name;
    	int age;
    	double score;
    };
    
    // 声明函数
    student updatedStudentInfo(student stu) 
    {
    	stu.age = 16;
    	stu.score = 98.1;
    	return stu;
    }
    
    int main()
    {
    	struct student stu = { "法外狂徒张三",18,98.8 };
    	student stuInfo = updatedStudentInfo(stu);
    	cout << "学生姓名: " << stuInfo.name << ",学生年龄:" << stuInfo.age << ",学生成绩: " << stuInfo.score << endl;
    	system("pause");
    	return 0;
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 地址传递
    #include
    using namespace std;
    
    // 结构体
    struct student {
    	string name;
    	int age;
    	double score;
    };
    
    // 声明函数
    
    student updatedStudentInfoPath(struct student* stu);
    
    
    student updatedStudentInfoPath(struct student* stu) 
    {
    	stu->age = 16;
    	stu->score = 96.8;
    	return *stu;
    }
    
    
    
    int main()
    {
    	struct student stu = { "法外狂徒张三",18,98.8 };
    
    	student* p = &stu;
    	student stuInfoP = updatedStudentInfoPath(p);
    	cout << "学生姓名: " << stuInfoP.name << ",学生年龄:" << stuInfoP.age << ",学生成绩: " << stuInfoP.score << endl;
    	cout << stu.score << endl; // 96.8
    
    	system("pause");
    	return 0;
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37

    8.7结构体中const使用场景

    • const来防止误操作

    8.7.1结构体中const使用场景演示

    #include
    using namespace std;
    #include
    
    struct student 
    {
    	string name;
    	int age;
    	float score;
    };
    
    
    // 声明函数
    
    // 打印学生信息
    void printStudentInfo(student stu);
    // 打印学生信息,采用指针可以节约内存开销
    void printStuInfoP(student* stu);
    
    
    // 函数实现
    void printStudentInfo(student stu) 
    {
    	cout << "学生姓名: " << stu.name << ",学生年龄:" << stu.age << ",学生成绩: " << stu.score << endl;
    }
    
    void printStuInfoP(const student* stu)
    {
    	// stu->name = "富甲一方田七"; // 加入const之后,一旦有修改的操作就会报错,可以防止误操作
    	cout << "学生姓名: " << stu->name << ",学生年龄:" << stu->age << ",学生成绩: " << stu->score << endl;
    }
    
    
    int main() 
    {
    	struct student stu = { "法外狂徒张三",23,94.3f };
    
    	printStudentInfo(stu);
    	printStuInfoP(&stu);
    	system("pause");
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42

    补充知识点:

    • 随机数种子
    #include
    srand((unsigned int)time(NULL));
    
    • 1
    • 2
  • 相关阅读:
    深入了解Spring循环依赖本质
    Flutter高仿微信-第31篇-单聊-表情
    深入JS内存模型
    Linux vi和vim编辑器、快捷键的使用
    第三章 UI组件之弹出组件【Android基础学习】
    RabbitMQ安装
    十三、函数式编程(1)
    emqx启用JWT令牌认证(包含hmac-based和public-key)
    使用人工智能自动测试 Flutter 应用程序
    深入理解WPF中MVVM的设计思想
  • 原文地址:https://blog.csdn.net/qq_39656068/article/details/126242860