C++进修——C++基础入门

初识C++

书写HelloWorld

#include <iostream>
using namespace std;

int main() {
	cout << "HelloWorldd" << endl;

	system("pause");

	return 0;
}

注释

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

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

提示:编译器在编译代码时,会忽略注释的内容

变量

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

语法数据类型 数据名 = 初始值

示例

#include <iostream>
using namespace std;

int main() {

	int a = 10;

	cout << "a = " << a << endl;

	system("pause");
	return 0;
}

常量

作用:用于记录程序中不可更改的数据
C++定义常量两种方式

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

示例:

#include <iostream>
using namespace std;

//宏常量
#define day 365

int main() {

	cout << "一年一共有" << day << "天" << endl;

	//const修饰的常量
	const int date = 7;

	cout << "一周一共有" << date << "天" << endl;

	system("pause");
	
	return 0;
}

关键字

作用:关键字是C++中预先保留的单词(标识符)

  • 在定义变量或者常量的时候,不要用关键字

C++关键字如下:

asmdoifreturntypeof
autodoubleinlineshorttypeid
booldynamic_castintsignedtypename
breakelselongsizeofunion
caseenummutablestaticunsinged
catchexplicitnamespacestatic_castusing
charexportnewstructvoid
constfalseprivatetemplatevolatile
const_castfloatprotectedthiswchar_t
continueforpublicthrowwhile
defaultfriendregistertrue
deletegotoreinterpret_casttry

提示:在给变量或者常量起名称的时候,不要用C++的关键字,否则会产生歧义

示例:

#include <iostream>
using namespace std;

int main() {

	//int int = 10;
	//第二个int是关键字,不可以做为变量的名称

	system("pause");
	return 0;
}

标识符命名规则

作用:C++规定给标识符(常量、变量)命名时,有一套自己的规则

  • 标识符不能是关键字
  • 标识符只能由字母、数字、下划线组成
  • 第一个字符必须为字母或下划线
  • 标识符中字母区分大小写

建议:给标识符命名时,争取做到见名知意的效果,方便自己和他人的阅读

数据类型

整型

作用:整型变量表示的是整数类型的数据
C++中能够表示整型的数据类型有以下几种方式,区别在于内存空间不同

数据类型占用空间取值范围
short(短整型)2字节 − 2 15 -2^{15} 215 ~ 2 15 2^{15} 215 - 1
int(整型)4字节 − 2 31 -2^{31} 231 ~ 2 31 2^{31} 231 - 1
long(长整型)Windows为4字节,Linux为4字节(32位),8字节(64位) − 2 31 -2^{31} 231 ~ 2 31 − 1 2^{31} - 1 2311
long long(长长整型)8字节 − 2 63   2 63 − 1 -2^{63} ~ 2^{63} - 1 263 2631

sizeof关键字

作用:利用sizeof关键字可以统计数据类型所占内存大小
语法sizeof(数据类型/变量)
示例

int main(){
	cout << "short类型所占内存空间为:" << sizeof(short) << endl;
	cout << "int类型所占内存空间为:" << sizeof(int) << endl;
	cout << "long类型所占内存空间为:" << sizeof(long) << endl;
	cout << "long long类型所占内存空间为:" << sizeof(long long) << endl;

	system("pause");
	return 0;
}

实型(浮点型)

作用:用于表示小数
浮点型变量分为两种:

  • 单精度float
  • 双精度double

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

数据类型占用空间有效数字范围
float4字节7位有效数字
double8字节15~16位有效数字

示例:

int main(){
	float f1 = 3.14f;
	double d1 = 3.14;

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

	cout << f1 << endl;
	cout << d1 << endl;

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

	system("pause");
	return 0;
}

字符型

作用:字符型变量用于显示单个字符
语法char ch = 'a'

注意1:在显示字符型变量时,用单引号将字符括起来,不要用双引号
注意2:单引号只能有一个字符,不能是字符串

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

示例

int main(){
	char ch = 'a';
	cout << ch << endl;
	cout << sizeof(char) << endl;
	
	//ch = "abcde" //错误,不可以用双引号
	//ch = 'abcde' //错误,单引号只能引用一个字符

	cout << (int)ch << endl;//查看字符ch对应的ASCII码
	ch = 97;//可以直接用ASCII给字符型变量赋值
	cout << ch << endl;

	system("pause");
	return 0;

ASCII码大知由以下两部分组成:

  • ASCII非打印字符:ASCII表上的数字0~31分配给了控制字符,用于控制像打印机等一些外围设备
  • ASCII打印字符:数字32~126分配给了能在键盘上找到的字符,当查看或打印文档时就会出现

转义字符

作用:用于表示一些不能显示出来的ASCII字符
现阶段常用的字符有:\n \\ \t

转义字符含义ASCII码值(十进制)
\a警报007
\b退格,将当前位置移到前一列008
\f换页,将当前位置移到下一页开头012
\n换行,将当前位置移到下一行开头010
\r回车,将当前位置移到本行开头013
\t水平制表(跳到下一个tab位置)009
\v垂直制表011
|代表一个反斜线字符""092
代表一个单引号字符039
"代表一个双引号字符034
?代表一个问号063
\0数字0000
\ddd8进制转义字符,d范围0~73位8进制
\xhh16进制转义字符,h范围 0 9 , a 0_{9,a} 09,af,A~F3位16进制

示例:

int main(){
	cout << "\n" << endl;
	cout << "\\" << endl;
	cout << "\tHello" << endl;

	system("pause");
	return 0;
}

字符串型

作用:用于表示一串字符
两种风格

  1. C风格字符串char 变量名[] = "字符串值"

示例:

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

	system("pause");
	return 0;
}

注意:C风格的字符串要用双引号括起来

  1. C++风格字符串string 变量名 = "字符串值"

示例:

//需要引用命名空间string,即
#include <string>

int main(){
	string str1 = "hello world";
	cout << str1 << endl;

	system("pause");
	return 0;
}

注意:C++风格字符串,需要加入头文件#include <string>

布尔类型 bool

作用:布尔数据类型代表真或假的值
bool类型只有两个值:

  • ture - 真(本质为1)
  • false - 假(本质为0)

bool类型占一个字节大小

示例:

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

	flag = flase;
	cout << "size of bool = " << sizeof(bool) << endl;

	system("pause");
	return 0;
}

注意:布尔类型非0的值都代表为true

数据的输入

作用:用于从键盘获取数据
关键字:cin
语法cin >> 变量

示例:

int main(){
	int a = 0;
	cout << "请输入整型变量" << endl;
	cin >> a;
	cout << a << endl;

	float b = 0;
	cout << "请输入浮点型变量" << endl;
	cin >> b;
	cout << b << endl;

	char ch = 'a';
	cout << "请输入字符型变量" << endl;
	cin >> ch;
	cout << ch << endl;

	string str = " ";
	cout << "请输入字符串变量" << endl;
	cin >> str;
	cout << str << endl;

	bool flag = false;
	cout << "请输入布尔变量" << endl;
	cin >> flag;
	cout << flag << endl;

	system("pause");
	return 0;

运算符

算术运算符

作用:用于处理四则运算

算术运算符包括以下符号:

运算符术语示例结果
+正号+33
-负号-3-3
+10 + 515
-10 - 55
*10 * 550
/10 / 52
%取模(取余)10 % 31
++前置递增a = 2,b = ++a;a = 3,b = 3
++后置递增a = 2,b = a++;a = 3,b = 2
前置递减a = 2,b = --a;a = 1,b = 1
后置递减a = 2,b = a–;a = 1,b = 2

示例1:

// 加减乘除
int main(){
	int a = 10;
	int b = 3;

	cout << a + b << endl;
	cout << a - b << endl;
	cout << a * b << endl;
	//结果向下取整
	cout << a / b << endl;

	int m = 10;
	int n = 20;

	cout << m / n << endl;

	int x = 10;
	int y = 0;
	//会报错,除数为0
	cout << x / y << endl;

	double c = 0.5;
	double d = 0.25;

	cout << c / d << endl;

	system("pause");
	return 0;
}

示例2:

//取模
int main(){
	int a = 10;
	int b = 3;

	cout << a % b << endl;

	int x = 10;
	int y = 20;

	cout << x % y << endl;
	
	int m = 10;
	int n = 0;
	//因为基于除法运算,所以依旧会报错
	cout << m % n << endl;

	double c = 3.14;
	double d = 1.1;
	//两个小数不可以进行取模运算
	cout << c % d << endl;

	system("pause");
	reutrn 0;
}

示例3:

//递增递减
int main(){
	int a = 10;
	a++;
	cout << a << endl;

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

	int c = 10;
	int d = c++ * 10;
	cout << c << endl;
	cout << d << endl;

	int e = 10;
	int f = ++e * 10;
	cout << e << endl;
	cout << f << endl;

	system("pause");
	return 0;
}

赋值运算符

作用:用于将表达式的值赋值给变量

赋值运算符包括以下几个符号

运算符术语示例结果
=赋值a = 2,b = 3a = 2,b = 3
+=加等于a = 0,a += 2a = 2
-=减等于a = 5,a -= 3a = 2
*=乘等于a = 2,a *= 2a = 4
/=除等于a = 4,a /= 2a = 2
%=模等于a = 3,a %= 2a = 1

示例:

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

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

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

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

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

	system("pause");
	return 0;
}

比较运算符

作用:用于表达式的比较,并返回一个真值或假值

比较运算符有以下符号

运算符术语示例结果
==相等于4 == 30
!=不等于4 != 31
<小于4 < 30
>大于4 > 31
<=小于等于4 <= 30
>=大于等于4 >= 31

示例

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;
}

逻辑运算符

作用:用于根据表达式的值返回真值或假值

逻辑运算符有以下符号

运算符术语示例结果
!!a如果a为假,则!a为真,反之同理
&&a && b如果a,b都为真,则结果才为真
||a || b如果a或b至少有一个为真,则结果为真

示例1:

//逻辑非
int main(){
	int a = 10;

	cout << !a << endl;
	cout << !!a << endl;

	system("pause");
	return 0;
}

示例2:

//逻辑与
int main(){
	int a = 10;
	int b = 10;

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

	b = 0;

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

	a = 0;

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

	system("pause");
	return 0;

示例3

//逻辑或
int main(){
	int a = 10;
	int b = 10;

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

	b = 0;

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

	a = 0;

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

	system("pause");
	return 0;
}

程序流程结构

选择结构

if语句

作用:执行满足条件的语句

if语句的三种形式

  • 单行格式if语句
  • 多行格式if语句
  • 多条件的if语句
  1. 单行格式if语句:if(条件)(条件满足执行的语句)

在这里插入图片描述

示例

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

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

	if(score > 600){
		cout << "恭喜你考上了一本" << endl;
	}
	
	system("pause");
	return 0;
}
  1. 多行格式if语句:if(条件){条件满足执行的语句}else{条件不满足执行的语句}

在这里插入图片描述

示例:

int main(){
	int score = 0;
	cout << "请输入考试分数" << endl;
	cin >> score;

	if(score > 600){
		cout << "恭喜考上了一本大学" << endl;
	}
	else{
		cout << "未考上一本大学" << endl;
	}

	system("pause");
	return 0;
}
  1. 多条件的if语句:if(条件1){条件1满足执行的语句}else if(条件2){条件2满足执行的语句}...else{都不满足执行的语句}

在这里插入图片描述

int main(){
	int score = 0;
	cout << "请输入考试分数" << endl;
	cin >> score;

	if(score > 600){
		cout << "考上了一本大学" << endl;
	}
	else if(score > 500){
		cout << "考上了二本大学" << endl;
	}
	else if(score > 400){
		cout << "考上了三本大学" << endl;
	}
	else{
		cout << "未考上大学" << endl;
	}

	system("pause");
	return 0;
}

嵌套if语句:在if语句中,可以嵌套使用if语句,达到更准确的条件判断

示例:

int main(){
	int score = 0;
	cout << "请输入考试分数" << endl;
	cin >> score;

	if(score > 600){
		if(score >700){
			cout << "考上了清华" << endl;
		}
		else if(score > 650){
			cout << "考上了北大" << endl;
		}
		else{
			cout << "考上了人大" << endl;
		}
	}
	else if(score > 500){
		cout << "考上了二本大学" << endl;
	}
	else if(score > 400){
		cout << "考上了三本大学" << endl;
	}
	else{
		cout << "未考上大学" << endl;
	}

	system("pause");
	return 0;
}

三目运算符

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

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

解释:如果表达式1为真,则执行表达式2,否则执行表达式3

示例:

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

	c = a > b ? a : b;
	cout << c << endl;

	(a > b ? a : b) = 100;

	cout << a << endl;
	cout << b << endl;

	system("pause");
	return 0;
}

switch语句

作用:执行多条件分支语句
语法

switch(表达式){
	case 结果1:执行语句;break;
	case 结果2:执行语句;break;
	...
	default:执行语句;break;
}

示例:

int main(){
	cout << "请给电影打分" << endl;

	int score = 0;
	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;
		default:
			cout << "烂片" << endl;
			break;
	}
	
	system("pause");
	return 0;
}

循环结构

while循环语句

作用:满足循环条件,执行循环语句

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

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

在这里插入图片描述

示例:

int main(){
	int num = 0;

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

	system("pause");
	return 0;
}

do…while循环语句

作用:满足循环条件,执行循环语句

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

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

在这里插入图片描述

示例:

int main(){
	int num = 0;

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

	system("pause");
	return 0;
}

for循环语句

作用:满足循环条件,执行循环语句

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

示例:

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

	system("pause");
	return 0;

嵌套循环

作用:在循环体中再嵌套一层循环,解决一些实际问题

示例:

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

	system("pause");
	return 0;
}

跳转语句

break语句

作用:用于跳出选择结构或者循环结构

break使用的时机:

  • 出现在switch条件语句中,作用是终止case并跳出switch
  • 出现在循环语句中,作用是跳出当前的循环语句
  • 出现在嵌套循环中,跳出最近的循环语句

示例:

int main(){
	cout << "请选择你挑战的副本" << endl;
	cout << "1. 普通" << endl;
	cout << "2. 中等" << endl;
	cout << "3. 困难" << endl;

	int num = 0;

	cin << num;

	switch(num){
		case 1:
			cout << "普通难度" << endl;
			break;
		case 2:
			cout << "中等难度" << endl;
			break;
		case 3:
			cout << "困难难度" << endl;
			break;
		default:
			cout << "参数错误" << endl;
			break;
	}

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

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

	system("pause");
	return 0;
}

continue语句

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

示例:

int main(){
	for(int i = 0; i < 100; i++){
		if(i % 2 == 0){
			continue;
		}
		cout << i << endl;
	}

	system("pause");
	return 0;
}

goto语句

作用:可以无条件语句

语法goto 标记

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

示例:

int main(){
	cout << "1" << endl;

	goto FLAG;

	cout << "2" << endl;
	cout << "3" << endl;
	cout << "4" << endl;

	FLAG;

	cout << "5" << endl;
	
	system("pause");
	return 0;
}

数组

概述

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

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

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

一维数组

一维数组定义方式

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

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

示例:

int main(){
	int score[10];

	score[0] = 100;
	score[1] = 99;
	score[2] = 85;

	cout << score[0] << endl;
	cout << score[1] << endl;
	cout << score[2] << endl;

	int score2[10] = {100,90,80,70,60,50,40,30,20,10};

	for(int i = 0; i < 10; i++){
		cout << score2[i] << endl;
	}

	int score3[] = {100,90,80,70,60,50,40,30,20,10};

	for(int i = 0; i < 10; i++){
		cout << score2[i] << endl;
	}

	system("pause");	
	return 0;
}

一维数组数组名

一维数组名称的用途

  • 可以统计整个数组在内存中的长度
  • 可以获取数组在内存中的首地址

示例:

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

冒泡排序

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

  • 比较相邻的元素。如果第一个比第二个大,就交换他们两个
  • 对每一组相邻元素做同样的工作,执行完毕后,找到第一个最大值
  • 重复以上的步骤,每次比较次数-1,直到不需要比较

示例:

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

	for(int i = 0; i < 8; i++){
		for(int j = 0; j < 8 - i;j++{
			if(arr[j] > arr[j + 1]){
				int temp = arr[j];
				arr[j] = arr[j + 1];
				arr[j + 1] = temp;
			}
		}
	}

	system("pause");
	return 0;
}

二维数组

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

二维数组定义方式

二维数组定义的四种方式:

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

示例:

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] << " ";
		}
		cout << endl;
	}

	int arr2[2][3] = {
		{1,2,3},
		{4,5,6}
	};

	int arr3[2][3] = {1,2,3,4,5,6};

	int arr4[][3] = {1,2,3,4,5,6};
	
	system("pause");
	retrun 0;
}

二维数组数组名

用途

  • 查看二维数组所占空间
  • 获取二维数组首地址

示例:

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[1] << endl;

	cout << "二维数组第一个元素地址" << &arr[0][0] << endl;
	cout << "二维数组第二个元素地址" << &arr[0][1] << endl;

	system("pause");
	return 0;
}

函数

概述

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

函数的定义

函数的定义一般主要有5个步骤:

  1. 返回值类型
  2. 函数名
  3. 参数表列
  4. 函数体语句
  5. return 表达式

语法

返回值类型 函数名(参数列表){
	函数体语句

	return表达式
}
  • 返回值类型:一个函数可以返回一个值。在函数定义中
  • 函数名:给函数起个名称
  • 参数列表:使用该函数时,传入的数据
  • 函数体语句:花括号内的代码,函数内需要执行的语句
  • return表达式:和返回值类型挂钩,函数执行完后,返回相应的数据

示例:

int add(int num1,int num2){
	int sum = num1 + num2;
	return sum;
}

函数的调用

功能:使用定义好的函数
语法函数名(参数)

示例:

int add(int num1, int num2){
	int sum = num1 + num2;
	return sum;
}

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

	int sum = add(a,b);

	cout << sum << endl;

	a = 100;
	b = 100;

	sum = add(a,b);

	cout << sum << endl;

	system("pause");
	return 0;
}

值传递

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

示例:

void swap(int num1,int num2){
	int temp = num1;
	num1 = num2;
	num2 = temp;
}

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

	swap(a,b);

	cout << a << endl;
	cout << b << endl;
	
	system("pause");
	return 0;
}

函数的常见样式

常见的函数样式有四种

  1. 无参无返
  2. 有参无返
  3. 无参有返
  4. 有参有返

示例:

void test01(){
	cout << "test01" << endl;
}

void test02(int num){
	cout << "test02" << num << endl;
}

int test03(){
	cout << "test03" << endl;
	return 10;
}

int test04(int a){
	cout << "test04" << a << endl;
	return a;
}

int main(){
	test01();
	
	test02(100);

	int num1 = test03();
	cout << num1 << endl;

	int num2 = test04();
	cout << num2 << endl;

	system("pause");
	return 0;
}

函数的声明

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

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

示例

int max(int a,int b);
int max(int a,int b);

int max(int a,int b){
	return a > b ? a : b;
}

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

	cout << max(a,b) << endl;

	system("pause");
	return 0;
}

函数的分文件编写

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

函数分文件编写一般有4个步骤

  1. 创建后缀名为.h的头文件
  2. 创建后缀名为.cpp的源文件
  3. 在头文件中写函数的声明
  4. 在源文件中写函数的定义

示例:

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

swap(int a,int b);

// swap.cpp
#include "swap.h"

void swap(int a,int b){
	int temp = a;
	a = b;
	b = temp;
}

指针

指针的基本概念

指针的作用:可以通过指针间接访问内存

  • 内存编号是从0开始记录的,一般用16进制数字表示
  • 可以利用指针变量保存地址

指针变量的定义和使用

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

示例:

int main(){
	int a = 10;

	int * p;

	p = &a;
	cout << &a << endl;
	cout << p << endl;

	cout << * p << endl;

	system("pause");
	return 0;
}

指针所占内存空间

在32位系统下,指针占4位字节空间大小,64位为8位字节空间大小

示例:

int main(){
	int a = 10;

	int * p = &a;

	cout << *p << endl;
	cout << sizeof(p) << endl;
	cout << sizeof(int *) << endl;
	cout << sizeof(double *) << endl;
	cout << sizeof(float *) << endl;
	cout << sizeof(char *) << endl;
	
	system("pause");
	return 0;
}

空指针和野指针

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

用途:初始化指针变量

注意:空指针指向的内存是不可以访问的,0~255号内存为系统占用内存

示例1:

int main(){
	int * p = NULL;

	//会报错
	cout << *p <<endl;

	system("pause");
	return 0;
}

野指针:指针变量指向非法的内存空间

示例2:

int main(){
	//指针变量指向内存编号为0x1100的空间
	int * p = (int *)0x1100;

	//报错
	cout << *p << endl;

	system("pause");
	return 0;
}

const修饰指针

const修饰指针有三种情况:

  1. const修饰指针——常量指针
  2. const修饰常量——指针常量
  3. const既修饰指针,又修饰常量

示例:

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

	//const修饰的是指针,指针的指向可以改,指针指向的值不可以修改
	const int * p1 = &a;
	p1 = &b;

	//报错
	*p1 = 100;

	//const修饰的是常量,指针指向不可以改,指针指向的值可以更改
	int * const p2 = &a;

	//报错
	p2 = &b;

	//const既修饰指针又修饰常量
	const int * const p3 = &a;

	//都报错
	p3 = &b;
	p3 = 20;

	system("pause");
	return 0;
}

指针和数组

作用:利用指针访问数组中元素

示例:

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

	int *p = arr;

	cout << "第一个元素" << arr[0] << endl;
	cout << "指针访问第一个元素" << *p << endl;

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

	system("pause");
	return 0;
}

指针和函数

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

示例:

//值传递
void swap1(int a,int b){
	int temp = a;
	a = b;
	b = temp;
}

//地址传递
void swap2(int * p1,int * p2){
	int temp = *p1;
	*p1 = *p2;
	*p2 = temp;
}

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

	//值传递不会改变实参
	swap1(a,b);

	//地址传递会改变实参
	swap2(a,b);

	cout << a << endl;
	cout << b << endl;

	system("pause");
	return 0;

指针,数组,函数

案例描述:封装一个函数,利用冒泡排序,实现对整形数组的升序排列

示例:

void bubbleSort(int * arr,int len){
	for(int i = 0;i < len - 1;i++){
		for(int j = 0;j < len - 1;j++{
			if(arr[j] > arr[j - 1]){
				int temp = arr[j];
				arr[j] = arr[j + 1];
				arr[j + 1] = temp;
			}
		}
	}
}

void printArray(int * arr,int len){
	for(int i = 0;i < len;i++){
		cout << arr[i] << endl;
	}
}

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

	int len = sizeof(arr) / sizeof(arr[0]);

	bubbleSort(arr,len);

	printArray(arr,len);

	system("pause");
	return 0;

结构体

结构体基本概念

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

结构体定义和使用

语法struct 结构体名 {结构体成员列表};

通过结构体创建变量的方式有三种:

  • struct 结构体名 变量名
  • struct 结构体名 变量名 = {成员1值,成员2值}
  • 定义结构体时顺便创建变量

示例:

struct student{
	string name;
	int age;
	int score;
};

int main(){
	student stu1;

	stu1.name = "张三";
	stu1.age = 18;
	stu1.score = 100;

	cout << stu1.name << endl;
	cout << stu1.age << endl;
	cout << stu1.score << endl;

	student stu2 = {"李四",19,60};

	cout << stu2.name << endl;
	cout << stu2.age << endl;
	cout << stu2.score << endl;

	system("pause");
	return 0;

结构体数组

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

语法struct 结构体名 数组名[元素个数] = {{},{},{}}

示例:

struct student{
	string name;
	int age;
	int score;
}

int main(){
	struct student arr[3] = {
		{"张三",18,80},
		{"李四",19,60},
		{"王五",20,88}
	}

	arr[2].name = "赵六";
	arr[2].age = 17;
	arr[2].score = 90;

	for(int i = 0;i < 3;i++){
		cout << stu[i].name << endl;
		cout << stu[i].age << endl;
		cout << stu[i].score << endl;
	}

	system("pause");
	return 0;

结构体指针

作用:通过指针访问结构体中的成员

  • 利用操作符 -> 可以通过结构体指针访问结构体属性

示例:

struct student{
	string name;
	int age;
	int score;
}

int main(){
	student stu = {"张三",18,100};

	student *p = &stu;

	p -> score = 80;

	cout << p -> name << endl;

	system("pause");
	return 0;
}

结构体嵌套结构体

作用:结构体中的成员可以是另一个结构体

示例:

struct student{
	string name;
	int age;
	int score;
};

struct teacher{
	int id;
	string name;
	int age;
	struct student stu;
};

int main(){
	teacher t1;
	t1.id = 10000;
	t1.name = "张三";
	t1.age = 40;

	t1.stu.name = "李四";
	t1.stu.age = 15;
	t1.stu.score = 59;

	cout << t1.id << endl;

	system("pause");
	return 0;
}

结构体做函数参数

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

传递方式有两种:

  • 值传递
  • 地址传递

示例:

struct student{
	string name;
	int age;
	int score;
};

void printStudent(student stu){
	stu.age = 28;
	cout << stu.age << endl;
}

void printStudent1(student *stu){
	stu -> age = 29;
	cout << stu -> age << endl;
}

int main(){
	student stu = {"张三",15,100};

	printStudent(stu);
	cout << stu.age << endl;

	printStudent2(&stu);
	cout << sut.age << endl;

	system("pause");
	return 0;
}

结构体中const使用场景

作用:用const来防止误操作

示例:

struct student{
	string name;
	int age;
	int score;
};

void printStudent(const student *stu){
	//无法操作,因为加了const修饰
	stu -> age = 199;
	cout << stu -> age << endl;
}

int main(){
	student stu = {"张三",15,199};

	printStudent(&stu);

	system("pause");
	return 0;
}
五",20,88}
	}

	arr[2].name = "赵六";
	arr[2].age = 17;
	arr[2].score = 90;

	for(int i = 0;i < 3;i++){
		cout << stu[i].name << endl;
		cout << stu[i].age << endl;
		cout << stu[i].score << endl;
	}

	system("pause");
	return 0;

结构体指针

作用:通过指针访问结构体中的成员

  • 利用操作符 -> 可以通过结构体指针访问结构体属性

示例:

struct student{
	string name;
	int age;
	int score;
}

int main(){
	student stu = {"张三",18,100};

	student *p = &stu;

	p -> score = 80;

	cout << p -> name << endl;

	system("pause");
	return 0;
}

结构体嵌套结构体

作用:结构体中的成员可以是另一个结构体

示例:

struct student{
	string name;
	int age;
	int score;
};

struct teacher{
	int id;
	string name;
	int age;
	struct student stu;
};

int main(){
	teacher t1;
	t1.id = 10000;
	t1.name = "张三";
	t1.age = 40;

	t1.stu.name = "李四";
	t1.stu.age = 15;
	t1.stu.score = 59;

	cout << t1.id << endl;

	system("pause");
	return 0;
}

结构体做函数参数

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

传递方式有两种:

  • 值传递
  • 地址传递

示例:

struct student{
	string name;
	int age;
	int score;
};

void printStudent(student stu){
	stu.age = 28;
	cout << stu.age << endl;
}

void printStudent1(student *stu){
	stu -> age = 29;
	cout << stu -> age << endl;
}

int main(){
	student stu = {"张三",15,100};

	printStudent(stu);
	cout << stu.age << endl;

	printStudent2(&stu);
	cout << sut.age << endl;

	system("pause");
	return 0;
}

结构体中const使用场景

作用:用const来防止误操作

示例:

struct student{
	string name;
	int age;
	int score;
};

void printStudent(const student *stu){
	//无法操作,因为加了const修饰
	stu -> age = 199;
	cout << stu -> age << endl;
}

int main(){
	student stu = {"张三",15,199};

	printStudent(&stu);

	system("pause");
	return 0;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/555865.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

二分法问题

日升时奋斗&#xff0c;日落时自省 目录 1、二分法 2、二分法问题 2.1 、在排序数组中查找元素的第一个和最后一个位置 2.2、搜索插入位置 2.3、山脉数组的峰顶索引 2.4、0-n-1中缺失的数字 1、二分法 二分法是比较简单的一种查找算法&#xff0c;但是效率很高&#xff0…

【创建型模式】原型模式

一、原型模式概述 原型&#xff08;Prototype&#xff09;模式的定义&#xff1a;用一个已经创建的实例作为原型&#xff0c;通过复制该原型对象来创建一个和原型相同或相似的新对象。在这里&#xff0c;原型实例指定了要创建的对象的种类。用这种方式创建对象非常高效&#xf…

【Qt】Qt安装包、源码、子模块(submodules)下载

1、Qt 4.0 ~ Qt5.14 Qt 4.0 ~ Qt5.14 离线安装包、源码和子模块(submodules)源码下载路径: https://download.qt.io/new_archive/qt/以Qt5.7.1为例,注意子模块都是源码,需要独立编译 2、Qt5.15 ~ Qt6.7 Qt5.15 ~ Qt6.7源码和子模块(submodules)源码下载路径: htt…

分类算法——决策树(五)

认识决策树 决策树思想的来源非常朴素&#xff0c;程序设计中的条件分支结构就是if-else结构&#xff0c;最早的决策树就是利用这类结构分割数据的一种分类学习方法。 决策树分类原理详解 为了更好理解决策树具体怎么分类的&#xff0c;通过一个问题例子&#xff1a; 问题…

【MIT6.824】lab3 Fault-tolerant Key/Value Service 实现笔记

引言 lab3A的实验要求如下&#xff1a; Your first task is to implement a solution that works when there are no dropped messages, and no failed servers. You’ll need to add RPC-sending code to the Clerk Put/Append/Get methods in client.go, and implement Pu…

HiveSql中的函数家族(二)

一、窗口函数 1、什么是窗口函数 在 SQL 中&#xff0c;窗口函数&#xff08;Window Functions&#xff09;是一种特殊的函数&#xff0c;它允许在查询结果集的特定窗口&#xff08;通常是一组行&#xff09;上执行聚合、分析和计算操作&#xff0c;而无需聚合整个结果集。窗口…

使用Python工具库SnowNLP对评论数据标注(二)

这一次用pandas处理csv文件 comments.csv import pandas as pd from snownlp import SnowNLPdf pd.read_csv("C:\\Users\\zhour\\Documents\\comments.csv")#{a: [1, 2, 3], b: [4, 5, 6], c: [7, 8, 9]}是个字典 emotions[] for txt in df[sentence]:s SnowNLP(…

接收区块链的CCF会议--ICSOC 2024 截止7.24

ICSOC是CCF B类会议&#xff08;软件工程/系统软件/程序设计语言&#xff09; 2023年长文短文录用率22% Focus Area 4: Emerging Technologies Quantum Service Computing Digital Twins 3D Printing/additive Manufacturing Techniques Blockchain Robotic Process Autom…

【QT+OpenCV】车牌号检测 学习记录 遇到的问题

【QTOpenCV】车牌号检测 学习记录 首先在QT里面配置好OpenCV .pro文件中加入&#xff1a; INCLUDEPATH G:/opencv/build/include LIBS -L"G:/opencv/build/x64/vc14/lib"\-lopencv_core \-lopencv_imgproc \-lopencv_highgui \-lopencv_ml \-lopencv_video \-lo.c…

Meta Llama 3强势来袭:迄今最强开源大模型,性能媲美GPT-4

前言 Meta的最新语言模型Llama 3已经发布&#xff0c;标志着在大型语言模型&#xff08;LLM&#xff09;领域的一次重大突破&#xff0c;其性能在行业内与GPT-4相媲美。此次更新不仅提升了模型的处理能力和精确性&#xff0c;还将开源模型的性能推向了一个新的高度。 Huggingf…

Docker八股总结

1. 容器和虚拟机的区别 传统虚拟机技术是虚拟出一套硬件后&#xff0c;在其上运行一个完整操作系统&#xff0c;在该系统上再运行所需应用进程&#xff1b;而容器内的应用进程直接运行于宿主的内核&#xff0c;容器内没有自己的内核&#xff0c;而且也没有进行硬件虚拟。因此容…

2021年全国大学生电子设计竞赛D题——基于互联网的摄像测量系统(二)

09 电路设计 前面介绍了系统的硬件框图如下&#xff1a; 硬件基本分为三块&#xff0c;两个摄像节点&#xff0c;一个终端节点。 1. 摄像节点硬件 摄像节点由一个DE10-Nano开发板和一个D8M摄像头实现&#xff0c;DE10-Nano开发板的HDMI接口外接HDMI显示器来显示拍摄到的视频。…

Flask + Bootstrap vs Flask + React/Vue:初学者指南

在这篇博客文章中&#xff0c;我们将比较 Flask Bootstrap 和 Flask React/Vue 这两种技术栈&#xff0c;以帮助初学者了解哪种组合更适合他们的项目需求。我们将从学习曲线、易用性、依赖管理、构建部署和路由定义等方面进行比较。 学习曲线 Flask 是一个基于 Python 的轻…

信息系统项目管理师0055:优化和持续改进(4信息系统管理—4.1管理方法—4.1.5优化和持续改进)

点击查看专栏目录 文章目录 4.1.5优化和持续改进1.定义阶段2.度量阶段3.分析阶段4.改进/设计阶段5.控制/验证阶段4.1.5优化和持续改进 优化和持续改进是信息系统管理活动中的一个环节,良好的优化和持续改进管理活动能够有效保障信息系统的性能和可用性等,延长整体系统的有效使…

偏微分方程算法之一阶双曲差分法

目录 一、研究目标 二、理论推导 2.1 引言 2.2 迎风格式 2.3 完全不稳定差分格式 2.4 蛙跳格式&#xff08;Leapfrog&#xff09; 2.5 Lax-Friedrichs格式 2.6 Lax-Wendroff格式 2.7 Beam-Warming格式 2.8 隐格式 2.9 Courant-Friedrichs-Lewy条件&#xff08;CFL条…

一文学会时序约束

主时钟约束命令/生成时钟约束命令IO输入输出延迟约束命令及效果最大最小延迟命令及作用多周期路径怎么约束什么情况设置伪路径时钟组设置的三个选项 如果不了解时序分析可以先看下下面这篇文章&#xff1a; 数字IC/FPGA——时序分析 目录 1.时钟约束&#xff08;1&#xff09;…

线性代数---行列式的性质

1. 行列式的行与列(按原顺序)互换

redis的数据结构报错

文章目录 redis的数据结构报错Redis使用LocalDateTime报错问题 redis的数据结构报错 Redis使用LocalDateTime报错问题 SpringBoot整合Redis时&#xff0c;使用LocalDate以下报错 org.springframework.data.redis.serializer.SerializationException: Could not read JSON: C…

数字时代安全风险防范与保密科技创新

文章目录 前言一、新技术应用带来的保密挑战1.1 通过技术手段获取国家秘密和重要情报日益普遍1.2 新型信息技术存在的风险不容忽视 二、加强保密科技创新的必要性2.1 提高定密准确性2.2 及时变更密级或解密2.3 对失泄密事故案件进行自动高效的预警和初步处理 三、保密科技创新中…

Jenkins机器已经安装了ansible, 运行的时候却报错ansible: command not found

操作系统&#xff1a;MacOS Jenkins log提示 ansible: command not found 直接在Jenkins 机器中&#xff0c;进入一样的目录执行ansible --version OK 原因&#xff1a; Jenkins 默认使用的环境是 /usr/bin, 而我的ansible 安装配置在conda3 下面&#xff0c;所以需要在Jenkin…
最新文章