笔记 C++ C++基础编程 小楼夜听雨 2024-05-07 2025-01-03 C++初识 注释
单行注释
多行注释
提示:编译器在编译代码时,会忽略注释的内容
变量
作用 :给一段指定的内存空间起名,方便操作这段内存
语法 :数据类型 变量名 = 初始值;
注意:C++在创建变量时,必须给变量一个初始值,否则会报错
常量 作用 :用于记录程序中不可更改的数据
C++定义常量两种方式
#define 宏常量: #define 常量名 常量值
const 修饰的变量 const 数据类型 常量名 = 常量值
通常在变量定义前加关键字const,修饰该变量为常量,不可修改
示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include <iostream> using namespace std;#define Day 7 int main2 () { cout << "一个星期=" << Day << "天" << endl; const int mooth = 12 ; cout << "一个年有" << mooth << "月" << endl; system ("pause" ); return 0 ; }
关键字
asm
do
if
return
typedef
auto
double
inline
short
typeid
bool
dynamic_cast
int
signed
typename
break
else
long
sizeof
union
case
enum
mutable
static
unsigned
catch
explicit
namespace
static_cast
using
char
export
new
struct
virtual
class
extern
operator
switch
void
const
false
private
template
volatile
const_cast
float
protected
this
wchar_t
continue
for
public
throw
while
default
friend
register
true
delete
goto
reinterpret_cast
try
标识符命名规则 作用 :C++规定给标识符(变量、常量)命名时,有一套自己的规则
标识符不能是关键字
标识符只能由字母、数字、下划线组成
第一个字符必须为字母或下划线
标识符中字母区分大小写
数据类型 整型 作用 :整型变量表示的是整数类型的数据
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)
示例:
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 #include <iostream> using namespace std;int main3 () { short a = 1 ; int b = 1 ; long c = 1 ; long long d = 1 ; cout << a << endl; cout << b << endl; cout << c << endl; cout << d << endl; system ("pause" ); return 0 ; }
sizeof关键字 作用: 利用sizeof关键字可以统计数据类型所占内存大小
语法: sizeof( 数据类型 / 变量)
示例:
1 2 3 4 5 6 7 8 9 10 11 12 #include <iostream> using namespace std;int main4 () { int a = 40 ; cout << "a所占用的空间为" << sizeof (a) << endl; system ("pause" ); return 0 ; }
实型(浮点型) 作用 :用于表示小数
浮点型变量分为两种:
单精度float
双精度double
两者的区别 在于表示的有效数字范围不同。
数据类型
占用空间
有效数字范围
float
4字节
7位有效数字
double
8字节
15~16位有效数字
示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 #include <iostream> using namespace std;int main5 () { float f1 = 3.14f ; double d1 = 3.14 ; cout << f1 << endl; cout << d1 << endl; cout << "float的字节是" << sizeof (float ) << endl; cout << "doubled的字节是" << sizeof (double ) << endl; float f2 = 3e2 ; cout << f2 << endl; float f3 = 3e-2 ; cout << f3 << endl; system ("pause" ); return 0 ; }
字符型 作用: 字符型变量用于显示单个字符
语法: char ch = 'a';
注意1:在显示字符型变量时,用单引号将字符括起来,不要用双引号
注意2:单引号内只能有一个字符,不可以是字符串
C和C++中字符型变量只占用1个字节。
字符型变量并不是把字符本身放到内存中存储,而是将对应的ASCII编码放入到存储单元
示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include <iostream> using namespace std;int main6 () { char ch = 'A' ; cout << ch << endl; cout << (int )ch << endl; system ("pause" ); return 0 ; }
转义字符 作用: 用于表示一些不能显示出来的ASCII字符
现阶段我们常用的转义字符有: \n \\ \t
转义字符
含义
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
\0
数字0
000
\ddd
8进制转义字符,d范围0~7
3位8进制
\xhh
16进制转义字符,h范围09,af,A~F
3位16进制
示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 int main7 () { int a = 2 ; cout << "hello word\n" ; cout << "aaa\tbbb\n" ; cout << "aaaaa\tbbb" << endl; cout << "\\" << endl;
字符串型 作用 :用于表示一串字符
两种风格
C风格字符串 : char 变量名[] = "字符串值"
C++风格字符串 : string 变量名 = "字符串值"
注意 :C++风格字符串,需要加入头文件#include
示例:
1 2 3 4 5 6 7 8 9 10 char A[] = "hello word" ;cout << A << endl; string B = "hello word" ; cout << B << endl;
布尔类型 bool 作用: 布尔数据类型代表真或假的值
bool类型只有两个值:
true — 真(本质是1)
false — 假(本质是0)
bool类型占1个字节大小
示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include <iostream> using namespace std;int main8 () { bool flag = true ; cout << flag << endl; flag = false ; cout << flag << endl; system ("pause" ); return 0 ; }
数据的输入 作用:用于从键盘获取数据
关键字: cin
语法: cin >> 变量
示例:
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 #include <iostream> #include <string> using namespace std;int main9 () { int a = 0 ; cout << "请给整型变量a赋值:" << endl; cin >> a; cout << "a=" << a << endl; double b = 3.14 ; cout << "请给浮点型b赋值:" << endl; cin >> b; cout << "b=" << b << endl; string c = "hello word" ; cout << "请给字符串c赋值:" << endl; cin >> c; cout << "c=" << c<< endl; char d = 'A' ; cout << "请给字符型d赋值:" << endl; cin >> d; cout << "d=" << d << endl; bool E = true ; cout << "请给布尔类型E赋值:" << endl; cin >> E; cout << "E=" << E << endl; system ("pause" ); return 0 ; }
运算符 作用: 用于执行代码的运算
本章我们主要讲解以下几类运算符:
运算符类型
作用
算术运算符
用于处理四则运算
赋值运算符
用于将表达式的值赋给变量
比较运算符
用于表达式的比较,并返回一个真值或假值
逻辑运算符
用于根据表达式的值返回真值或假值
算术运算符 作用 :用于处理四则运算
算术运算符包括以下符号:
运算符
术语
示例
结果
+
正号
+3
3
-
负号
-3
-3
+
加
10 + 5
15
-
减
10 - 5
5
*
乘
10 * 5
50
/
除
10 / 5
2
%
取模(取余)
10 % 3
1
++
前置递增
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 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 #include <iostream> using namespace std;int main1 () { int a1 = 3 ; int b1 = 10 ; cout << a1 + b1 << endl; cout << a1 - b1 << endl; cout << a1 * b1 << endl; cout << a1 / b1 << endl; double a2 = 0.5 ; double b2 = 0.6 ; cout << a2 / b2 << endl; cout << b1 % a1 << endl; cout << a1 % b1 << endl; int a = 10 ; int b = ++a * 10 ; cout << a << endl; cout << b << endl; int c = 10 ; int d = c++ * 10 ; cout << c << endl; cout << d << endl; system ("pauce" ); return 0 ; }
赋值运算符 作用: 用于将表达式的值赋给变量
赋值运算符包括以下几个符号:
运算符
术语
示例
结果
=
赋值
a=2; b=3;
a=2; b=3;
+=
加等于
a=0; a+=2;
a=2;
-=
减等于
a=5; a-=3;
a=2;
*=
乘等于
a=2; a*=2;
a=4;
/=
除等于
a=4; a/=2;
a=2;
%=
模等于
a=3; a%2;
a=1;
示例:
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 #include <iostream> using namespace std;int main2 () { int a = 10 ; a = 100 ; cout << a << endl; a = 10 ; a += 8 ; cout << a << endl; a = 10 ; a -= 8 ; cout << a << endl; a = 10 ; a *= 10 ; cout << a << endl; a = 10 ; a /= 5 ; cout << a << endl; a = 10 ; a %= 2 ; cout << a << endl; system ("pause" ); return 0 ; }
比较运算符 作用: 用于表达式的比较,并返回一个真值或假值
比较运算符有以下符号:
运算符
术语
示例
结果
==
相等于
4 == 3
0
!=
不等于
4 != 3
1
<
小于
4 < 3
0
>
大于
4 > 3
1
<=
小于等于
4 <= 3
0
>=
大于等于
4 >= 1
1
示例:
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 #include <iostream> using namespace std;int main3 () { 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为真,则!a为假。
&&
与
a && b
如果a和b都为真,则结果为真,否则为假。
||
或
a || b
如果a和b有一个为真,则结果为真,二者都为假时,结果为假。
示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include <iostream> using namespace std;int main () { int a = 10 ; cout << !a <<endl; cout << !!a << endl; int b = 10 ; cout <<( a && b )<< endl; int c = 0 ; cout << (a || c) << endl; system ("pause" ); return 0 ; }
程序流程结构 C/C++支持最基本的三种程序运行结构:顺序结构、选择结构、循环结构
顺序结构:程序按顺序执行,不发生跳转
选择结构:依据条件是否满足,有选择的执行相应功能
循环结构:依据条件是否满足,循环多次执行某段代码
选择结构 if语句 作用: 执行满足条件的语句
if语句的三种形式
单行格式if语句:if(条件){ 条件满足执行的语句 }
多行格式if语句:if(条件){ 条件满足执行的语句 }else{ 条件不满足执行的语句 };
多条件的if语句:if(条件1){ 条件1满足执行的语句 }else if(条件2){条件2满足执行的语句}... else{ 都不满足执行的语句}
示例:
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 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 #include <iostream> using namespace std;int main1 () { cout << "请输入你的分数" << endl; int a; cin >> a; if (a > 650 ) { cout << "恭喜考上清华大学!" << endl; } else { cout << "很遗憾您没有考上清华大学" << endl; } cout << "请输入你的分数" << endl; int b; cin >> b; if (b < 100 ) { cout << "很遗憾您没有考上大学" << endl; } else { if ((b>100 )&&(b<450 )){ cout << "恭喜您考上了大专" << endl; } else { if ((b > 450 ) && (b < 650 )) { cout << "恭喜您考上了本科" << endl; } else cout << "恭喜您考上了国本" << endl; } } cout << "请输入你的分数" << endl; cin >> b; if (b < 100 ) { cout << "很遗憾您没有考上大学" << endl; } else if (b<450 ) { cout << "恭喜您考上了大专" << endl; } else if (b<650 ){cout << "恭喜您考上了本科" << endl;} else { cout << "恭喜您考上了国本" << endl; } cout << "请输入你的分数" << endl; cin >> b; if (b < 100 ) { cout << "很遗憾您没有考上大学" << endl; } else if (b < 450 ) { cout << "恭喜您考上了大专" << endl; } else if (b < 650 ) { cout << "恭喜您考上了本科" << endl; } else { cout << "恭喜您考上了国本" << endl; if (b < 670 ) { cout << "恭喜考上人大!" << endl; } else if (b<700 ) { cout << "恭喜考上清华!" << endl; } else { cout << "恭喜考上北大!" << endl; } } system ("pause" ); return 0 ; }
三目运算符 作用: 通过三目运算符实现简单的判断
语法: 表达式1 ? 表达式2 :表达式3
解释:
如果表达式1的值为真,执行表达式2,并返回表达式2的结果;
如果表达式1的值为假,执行表达式3,并返回表达式3的结果。
示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 #include <iostream> using namespace std;int main3 () { int a=10 , b=20 , c=0 ; a > b ? c = a : c = b; cout << c << endl; c = (a > b ? a : b); cout << c << endl; (a > b ? a : b) = 100 ; cout << a << endl; cout << b << endl; system ("pause" ); return 0 ; }
switch语句 作用: 执行多条件分支语句
示例:
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 <iostream> using namespace std;int main4 () { cout << "请你为该电影打分:" << endl; int a = 0 ; cin >> a; switch (a) { 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 ; default : cout << "您认为是烂片" << endl; break ; } system ("pause" ); return 0 ; }
循环结构 while循环语句 作用: 满足循环条件,执行循环语句
语法: while(循环条件){ 循环语句 }
解释: 只要循环条件的结果为真,就执行循环语句
注意 :在执行循环语句时候,程序必须提供跳出循环的出口,否则出现死循环
示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 #include <iostream> using namespace std;int main5 () { int a = 0 ; while (a<10 ) { cout << a << endl; a++; } system ("pauce" ); return 0 ; }
do…while循环语句 作用: 满足循环条件,执行循环语句
语法: do{ 循环语句 } while(循环条件);
注意:与while的区别在于do…while会先执行一次循环语句,再判断循环条件
示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include <iostream> using namespace std;int main7 () { int a = 0 ; do { cout << a << endl; a++; } while (a < 10 ); system ("pauce" ); return 0 ; }
for循环语句 作用: 满足循环条件,执行循环语句
语法: for(起始表达式;条件表达式;末尾循环体) { 循环语句; }
示例:
1 2 3 4 5 6 7 8 9 10 11 #include <iostream> using namespace std;int main9 () { for (int a = 0 ; a < 10 ; a++) { cout << a << 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 #include <iostream> using namespace std;int main10 () { for (int a = 0 ; a < 11 ; a++) { cout << " *" ; }cout<<endl; for (int b = 0 ; b < 10 ; b++) { for (int a = 0 ; a < 11 ; a++) { cout << " *" ; }cout << endl; } system ("pauce" ); return 0 ; }
跳转语句 break语句 作用: 用于跳出选择结构或者循环结构
break使用的时机:
出现在switch条件语句中,作用是终止case并跳出switch
出现在循环语句中,作用是跳出当前的循环语句
出现在嵌套循环中,跳出最近的内层循环语句
示例:
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 #include <iostream> using namespace std;int main12 () { cout << "请选择你的难度" << endl; cout << "1.简单" << endl; cout << "2.中等" << endl; cout << "3.困难" << endl; int a; cin >> a; switch (a) { case 1 :cout << "你选择了简单难度" << endl; break ; case 2 :cout << "你选择了中等难度" << endl; break ; case 3 :cout << "你选择了困难难度" << endl; break ; } for (int a = 0 ; a < 10 ; a++) { if (a == 5 ) { break ; } cout << "*" << endl; } for (int b = 0 ; b < 10 ; b++) { for (int c = 0 ; c < 10 ; c++) { if (c == 5 ) { break ; }cout << "*" ; } cout << endl; } system ("pauce" ); return 0 ; }
continue语句 作用: 在循环语句中,跳过本次循环中余下尚未执行的语句,继续执行下一次循环
筛选条件,执行到此就不再向下
注意:continue并没有使整个循环终止,而break会跳出循环
示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include <iostream> using namespace std;int main13 () { for (int a = 0 ; a < 100 ; a++) { if (a % 2 == 0 ) { continue ; } cout << a << endl; } system ("pause" ); return 0 ; }
goto语句 作用: 可以无条件跳转语句
语法: goto 标记;
解释: 如果标记的名称存在,执行到goto语句时,会跳转到标记的位置
示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include <iostream> using namespace std;int main () { cout << "1" << endl; cout << "2" << endl; goto ABC; cout << "3" << endl; ABC: cout << "4" << endl; system ("pause" ); return 0 ; }
数组 概述 所谓数组,就是一个集合,里面存放了相同类型的数据元素
特点1: 数组中的每个数据元素都是相同的数据类型
特点2: 数组是由连续的内存位置组成的
一维数组 一维数组定义方式 一维数组定义的三种方式:
数据类型 数组名[ 数组长度 ];
数据类型 数组名[ 数组长度 ] = { 值1,值2 ...};
数据类型 数组名[ ] = { 值1,值2 ...};
示例:
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 #include <iostream> using namespace std;int main1 () { int arr[5 ]; arr[0 ] = 10 ; arr[1 ] = 20 ; arr[2 ] = 30 ; arr[3 ] = 40 ; arr[4 ] = 50 ; cout << arr[2 ] << endl; int A[5 ] = { 10 ,20 ,30 ,40 ,50 }; cout << A[2 ] << endl; int B[] = { 10 ,20 ,30 ,40 ,50 }; cout << B[2 ] << endl; for (int i=0 ; i < 5 ; i++) { cout << B[i] << 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 #include <iostream> using namespace std;int main3 () { int arr[5 ] = { 1 ,2 ,3 ,4 ,5 }; cout<<sizeof (arr)<< endl; cout<<sizeof (arr[2 ])<< endl; cout << sizeof (arr) / sizeof (arr[0 ]) << endl; cout << arr << endl; cout << (int ) arr << endl; cout <<(int )&arr[0 ]<<endl; system ("pause" ); return 0 ; }
注意:数组名是常量,不可以赋值
总结1:直接打印数组名,可以查看数组所占内存的首地址
总结2:对数组名进行sizeof,可以获取整个数组占内存空间的大小
冒泡排序 作用: 最常用的排序算法,对数组内元素进行排序
比较相邻的元素。如果第一个比第二个大,就交换他们两个。
对每一对相邻元素做同样的工作,执行完毕后,找到第一个最大值。
重复以上的步骤,每次比较次数-1,直到不需要比较
示例:
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 #include <iostream> using namespace std;int main5 () { int c = 0 ; int arr[10 ] = { 7 ,4 ,1 ,8 ,5 ,2 ,9 ,6 ,3 ,0 }; int d = sizeof (arr) / sizeof (arr[0 ])-1 ; for (; d > 0 ; d--) { for (int a = 0 , b = 1 ; b < 10 ; a++, b++) { if (arr[a] > arr[b]) { c = arr[b]; arr[b] = arr[a]; arr[a] = c; } } } for (int f = 0 ; f < 10 ; f++) { cout << arr[f] << endl; } int h = 0 ; int arr1[10 ] = { 7 ,4 ,1 ,8 ,5 ,2 ,9 ,6 ,3 ,0 }; int g = sizeof (arr1) / sizeof (arr1[0 ]); for (int a = 0 ; a < 9 ; a++) { for (int b = 1 ; g - b > 0 ; b++) { if (arr1[b - 1 ] > arr1[b]) { h = arr1[b - 1 ]; arr1[b - 1 ] = arr1[b]; arr1[b] = h; } } } for (int f = 0 ; f < 10 ; f++) { cout << arr1[f] << endl; } system ("pause" ); return 0 ; }
二维数组 二维数组就是在一维数组上,多加一个维度。
二维数组定义方式 二维数组定义的四种方式:
数据类型 数组名[ 行数 ][ 列数 ];
数据类型 数组名[ 行数 ][ 列数 ] = { {数据1,数据2 } ,{数据3,数据4 } };
数据类型 数组名[ 行数 ][ 列数 ] = { 数据1,数据2,数据3,数据4};
数据类型 数组名[ ][ 列数 ] = { 数据1,数据2,数据3,数据4};
建议:以上4种定义方式,利用第二种更加直观,提高代码的可读性
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 #include <iostream> using namespace std;int main6 () { int arr[3 ][3 ]; arr [0 ][0 ] = 1 ; arr[0 ][1 ] = 2 ; int arr1[2 ][3 ] = { {1 ,2 ,3 }, {4 ,5 ,6 } }; for (int a = 0 ; a < 2 ; a++) { for (int b = 0 ; b < 3 ; b++) { cout << arr1[a][b] << " " ; }cout << endl; } cout << arr1[0 ][0 ] << endl; int arr2[2 ][3 ] = { 1 ,2 ,3 ,4 ,5 ,6 }; int arr3[][3 ] = { 1 ,2 ,3 ,4 ,5 ,6 }; cout << sizeof (arr1) << endl; cout << sizeof (arr1[0 ]) << endl; cout << sizeof (arr1[0 ][0 ]) << endl; cout << sizeof (arr1[0 ])/sizeof (arr1[0 ][0 ]) << endl; cout << sizeof (arr1) / sizeof (arr1[0 ][0 ]) << endl; cout <<(int )arr1 << endl; cout << (int )arr1[0 ] << endl; cout << (int )&arr1[0 ][0 ] << endl; system ("pause" ); return 0 ; }
二维数组数组名
示例:
1 2 3 4 5 6 7 8 9 10 11 cout << sizeof (arr1) << endl; cout << sizeof (arr1[0 ]) << endl; cout << sizeof (arr1[0 ][0 ]) << endl; cout << sizeof (arr1[0 ])/sizeof (arr1[0 ][0 ]) << endl; cout << sizeof (arr1) / sizeof (arr1[0 ][0 ]) << endl; cout <<(int )arr1 << endl; cout << (int )arr1[0 ] << endl; cout << (int )&arr1[0 ][0 ] << endl;
总结1:二维数组名就是这个数组的首地址
总结2:对二维数组名进行sizeof时,可以获取整个二维数组占用的内存空间大小
函数 函数的定义 函数的定义一般主要有5个步骤:
1、返回值类型
2、函数名
3、参数表列
4、函数体语句
5、return 表达式
作用:将一段经常使用的代码封装起来;减少重复代码,
一个较大的程序,一般分为若干个程序块,每个模块实现特定的功能
示例:
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 #include <iostream> using namespace std;int add1 (int num1, int num2) { int sum = num1 + num2; return sum; } int main1 () { 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 #include <stdio.h> #include <iostream> using namespace std;int add2 (int num1, int num2) { int sum = num1 + num2; return sum; } int main2 () { int a = 10 ; int b = 20 ; int c = add2 (a, b); cout << c; 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 #include <stdio.h> #include <iostream> using namespace std;void swap1 (int num1, int num2) { cout << "交换前a=" << num1 << endl; cout << "交换前b=" << num2 << endl; int c = 0 ; c = num1, num1 = num2, num2 = c; cout << "交换后a=" << num1 << endl; cout << "交换后b=" << num2 << endl; } int main3 () { int a = 10 ; int b = 20 ; swap1 (a, b); cout << a << endl; cout << b << endl; system ("pause" ); return 0 ; }
函数的常见样式 常见的函数样式有4种
无参无返
无参有返
有参无返
有参有返
示例:
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 65 #include <stdio.h> #include <iostream> using namespace std;void A () { cout << "66666" << endl; } int B () { cout << "55555" << endl; return 1000 ; } void C (int a) { cout << "this is C=" << a << endl; } int D (int c) { int f = c * 5 ; return f; } int main4 () { A (); int b = B (); cout << b << endl; C (100 ); int g = D (5 ); cout << g << 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 #include <stdio.h> #include <iostream> using namespace std;int max (int a, int b) ;int main6 () { cout << max (500 , 600 ) << endl; system ("pause" ); return 0 ; } int max (int a, int b) { a < b ? b : a; return a < b ? b : a; }
函数的分文件编写 作用: 让代码结构更加清晰
函数分文件编写一般有4个步骤
创建后缀名为.h的头文件
创建后缀名为.cpp的源文件
在头文件中写函数的声明
在源文件中写函数的定义
1 2 3 4 5 #pragma once #include <iostream> using namespace std;void swap (int a, int b) ;
1 2 3 4 5 6 7 8 9 #include "swap.h" void swap (int a, int b) { int c; c = a; a = b; b = c; cout << a << endl; cout << b << endl; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 #include <iostream> #include "swap.h" using namespace std;int main () { int a = 10 ; int b = 20 ; swap (a, b); system ("pause" ); return 0 ; }
指针 指针的基本概念 指针的作用: 可以通过指针间接访问内存(当数组储存的数据过大的时候,可以通过指针减少全部引用复制)
内存编号是从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 #include <iostream> using namespace std;int main1 () { int a = 10 ; int * p; p = &a; cout << "a的地址为:" << &a << endl; cout << "指针p的地址:" << p << endl; *p = 1000 ; cout << a << endl; cout << sizeof (int *) << 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 cout << sizeof (int *) << endl; int main () { int a = 10 ; int * p; p = &a; cout << *p << endl; cout << sizeof (p) << endl; cout << sizeof (char *) << endl; cout << sizeof (float *) << endl; cout << sizeof (double *) << endl; system ("pause" ); return 0 ; }
空指针和野指针 空指针 :指针变量指向内存中编号为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 #include <iostream> using namespace std;int main2 () { int * p = NULL ; int * pp = (int *)0x123456 ; system ("pause" ); return 0 ; }
空指针和野指针都不是我们申请的空间,因此不要访问
const修饰指针 const修饰指针有三种情况
const修饰指针 — 常量指针
const修饰常量 — 指针常量
const即修饰指针,又修饰常量
示例:
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 #include <iostream> using namespace std;int main3 () { int a = 10 ; int b = 20 ; const int * p = &a; p = &b; cout << *p; int * const pp = &a; *pp = 30 ; const int * const ppp = &a; system ("pause" ); return 0 ; }
指针和数组 作用: 利用指针访问数组中元素
示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include <iostream> using namespace std;int main4 () { int arr[] = { 1 ,2 ,3 ,4 ,5 ,6 ,7 ,8 ,9 ,10 }; int * p = arr; cout << *p << endl; p++; 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 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 #include <iostream> using namespace std;void swap1 (int a1, int b1) { int c; c = a1; a1 = b1; b1 = c; } void swap2 (int * p1, int * p2) { int f; f = *p1; *p1 = *p2; *p2 = f; } int main6 () { int a = 10 ; int b = 20 ; swap2 (&a, &b); cout << a << endl << b << endl; system ("pause" ); return 0 ; }
总结:如果不想修改实参,就用值传递,如果想修改实参,就用地址传递
指针、数组、函数 案例描述: 封装一个函数,利用冒泡排序,实现对整型数组的升序排序
例如数组:int arr[10] = { 4,3,6,9,1,2,10,8,7,5 };
示例:
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 void max (int *ARR , int log) { for (int a=0 ;a<log-1 ;a++) { for (int b = 0 ; b < log - a - 1 ; b++) { int temp = 0 ; if (ARR[b] > ARR[b + 1 ]) { temp = ARR[b]; ARR[b] = ARR[b+1 ]; ARR[b+1 ] = temp; } } } } int main () { int arr[] = { 1 ,4 ,7 ,8 ,5 ,2 ,9 ,10 ,6 ,3 }; int arr1[] = { 56 ,5 ,89 ,4 ,564 ,48 ,54 ,5 ,96 ,52 ,3 ,56 ,74 ,52 }; void max (int * arr, int log) ; int log = sizeof (arr) / sizeof (arr[0 ]); int log1 = sizeof (arr1) / sizeof (arr1[0 ]); max (arr, log); for (int a = 0 ; a < log; a++) { cout << arr[a] << endl; } max (arr1, log1); for (int a = 0 ; a < log1; a++) { cout << arr1[a] << endl; } system ("pause" ); return 0 ; }
总结:当数组名传入到函数作为参数时,被退化为指向首元素的指针
结构体 结构体基本概念 结构体属于用户自定义的数据类型,允许用户存储不同的数据类型
结构体定义和使用 语法: struct 结构体名 { 结构体成员列表 };
通过结构体创建变量的方式有三种:
struct 结构体名 变量名
struct 结构体名 变量名 = { 成员1值 , 成员2值…}
定义结构体时顺便创建变量
示例:
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 #include <iostream> #include <string> using namespace std;int main1 () { struct student { string name; int age; int score; }s3; student s1; s1.name = "张三" ; s1.age = 14 ; s1.score = 78 ; cout << " 姓名:" << s1.name << " 年龄:" << s1.age << " 成绩:" << s1.score << endl; struct student s2 = { "李四" ,11 ,79 }; cout << " 姓名:" << s2.name << " 年龄:" << s2.age << " 成绩:" << s2.score << endl; s3.name = "王五" ; s3.age = 13 ; s3.score = 100 ; cout << " 姓名:" << s3.name << " 年龄:" << s3.age << " 成绩:" << s3.score << endl; system ("pause" ); return 0 ; }
总结1:定义结构体时的关键字是struct,不可省略
总结2:创建结构体变量时,关键字struct可以省略
总结3:结构体变量利用操作符 ‘’.‘’ 访问成员
结构体数组 作用: 将自定义的结构体放入到数组中方便维护
语法: struct 结构体名 数组名[元素个数] = { {} , {} , ... {} }
示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 #include <iostream> #include <string> using namespace std;int main2 () { struct student { string name; int age; int score; }; struct student arr[] = { {"张三" ,15 ,85 },{"李四" ,17 ,90 }, {"王五" ,17 ,100 } }; arr[2 ].name = "老六" ; for (int a = 0 ; a < 3 ; a++) { cout << "姓名:" << arr[a].name << " 年龄:" << arr[a].age << " 成绩:" << arr[a].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 #include <iostream> #include <string> using namespace std;struct student { string name; int age; int score; };int main3 () { student s = { "张三" ,18 ,80 }; student* p = &s; cout << "姓名:" << p->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 #include <iostream> #include <string> using namespace std;struct student { string name; int age; int score; };struct teacher { string name; int DH; struct student stu; };int main5 () { teacher A; A.name = "老王" ; A.DH = 18813379157 ; A.stu.name = "张三" ; A.stu.age = 18 ; A.stu.score = 99 ; cout << " 姓名:" << A.name << " 电话:" << A.DH << " 学生:" << A.stu.name <<" 学生年龄:" <<A.stu.age<<" 学生成绩" <<A.stu.score << endl; student C{ "张三" ,8 ,100 }; teacher B{ "老王" ,18813379175 ,C }; cout << " 姓名:" << B.name << " 电话:" << B.DH << " 学生:" << B.stu.name << " 学生年龄:" << B.stu.age << " 学生成绩" << B.stu.score << endl; system ("pause" ); return 0 ; }
我的方法还没得到验证,不知有student D时会访问到谁的
结构体做函数参数 作用: 将结构体作为参数向函数中传递
传递方式有两种:
示例:
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 <iostream> #include <string> using namespace std;struct student { string name; int age; int score; };void daying (student A) { cout << "姓名:" << A.name << " 年龄:" << A.age << " 成绩:" << A.score << endl; } void daying2 (student* A) { A->age = 20 ; cout << "姓名:" << A->name << " 年龄:" << A->age << " 成绩:" << A->score << endl; } int main4 () { student s{ "张三" ,18 ,98 }; daying (s); daying2 (&s); cout <<"在地址传递函数中修改后的年龄:" << s.age << endl; system ("pause" ); return 0 ; }
结构体中 const使用场景 作用: 用const来防止误操作
示例:
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 #include <iostream> #include <string> using namespace std;struct student1 { string name; int age; int score; };void daying (student1 A) { cout << "姓名:" << A.name << " 年龄:" << A.age << " 成绩:" << A.score << endl; } void daying2 (const student1* A) { cout << "姓名:" << A->name << " 年龄:" << A->age << " 成绩:" << A->score << endl; } int main6 () { student1 s{ "张三" ,18 ,98 }; system ("pause" ); return 0 ; }