下面是c++的基础编程笔记,视频链接:黑马程序员匠心之作|C++教程从0到1入门编程,学习编程不再难_哔哩哔哩_bilibili
需要pdf版的点击下载:https://kblog.ink?download=193&tmstv=1764264298
C++ 初识
注释(Comments)
作用
在代码中加入说明、解释,方便自己或他人阅读。
两种格式
1. 单行注释
// 描述信息
用途:
- 放在一行代码的上方(说明这段代码)
- 或放在语句末尾(说明当前行)
2. 多行注释
/* 描述信息 */
用于对一段代码进行整体说明。
注意事项
- 编译器会忽略注释内容,不参与编译。
变量(Variable)
作用
给一段内存空间起名,用于存储数据。
语法
数据类型 变量名 = 初始值;
示例
#include<iostream>
using namespace std;
int main() {
int a = 10;
cout << "a = " << a << endl;
system("pause");
return 0;
}
注意事项
- C++ 必须给变量初始化,否则将产生错误。
常量(Constant)
作用
记录不可更改的数据。
两种常量定义方法
宏常量(#define)
语法:
#define 常量名 常量值
通常写在文件上方。
示例:
#define day 7
int main() {
cout << "一周里总共有 " << day << " 天" << endl;
// day = 8; // 错误:宏常量不能修改
system("pause");
return 0;
}
const 修饰变量
语法:
const 数据类型 常量名 = 常量值;
示例:
const int month = 12;
cout << "一年有 " << month << " 月份" << endl;
// month = 24; // 错误:常量不可修改
关键字(Keyword)
作用
关键字是 C++ 中被保留的标识符,不能用于变量名或常量名。
C++ 全部关键字(PDF原样)
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
标识符命名规则
- 不能使用关键字
- 只能由字母、数字、下划线组成
- 不能以数字开头
- 区分大小写
- 建议见名知意
数据类型(Data Types)
C++ 创建变量或常量时必须指定数据类型,否则无法分配内存。
整型(Integer)
表示整数。
| 类型 | 占用空间 | 取值范围 |
|---|---|---|
| short | 2 字节 | -2^15 ~ 2^15-1 |
| int | 4 字节 | -2^31 ~ 2^31-1 |
| long | Windows 4 字节,Linux (32bit=4、64bit=8) | -2^31 ~ 2^31-1 |
| long long | 8 字节 | -2^63 ~ 2^63-1 |
结论
short < int <= long <= long long
浮点型(float / double)
用于表示小数。
| 类型 | 占用空间 | 有效数字 |
|---|---|---|
| float | 4 字节 | 7 位 |
| double | 8 字节 | 15-16 位 |
示例
float f1 = 3.14f;
double d1 = 3.14;
cout << f1 << endl;
cout << d1 << endl;
cout << "float sizeof = " << sizeof(f1) << endl;
cout << "double sizeof = " << sizeof(d1) << endl;
// 科学计数法
float f2 = 3e2; // 300
float f3 = 3e-2; // 0.03
sizeof 关键字
作用:查看类型或变量占用内存
语法:
sizeof(类型)
sizeof(变量)
示例:
cout << sizeof(short) << endl;
cout << sizeof(int) << endl;
cout << sizeof(long) << endl;
cout << sizeof(long long) << endl;
字符型(char)
表示单个字符,占 1 字节,存储的是 ASCII 值。
语法
char ch = 'a';
注意事项
- 必须使用 单引号
- 单引号内只有一个字符
- 实际存储 ASCII 码
示例
char ch = 'a';
cout << ch << endl;
cout << sizeof(char) << endl;
cout << (int)ch << endl;
ch = 97;
cout << ch << endl;
转义字符
| 转义 | 含义 |
|---|---|
| \n | 换行 |
| \t | 制表符 |
| \ | 反斜杠 |
| ‘ | 单引号 |
| “ | 双引号 |
| ? | 问号 |
| \0 | 数字 0 |
示例:
cout << "\\" << endl;
cout << "\tHello" << endl;
cout << "\n" << endl;
字符串
C 风格字符串
char str[] = "hello world";
C++ 风格字符串
#include <string>
string str = "hello world";
布尔类型(bool)
- true = 1
- false = 0
- 占 1 字节
示例:
bool flag = true;
cout << flag << endl; // 1
flag = false;
cout << flag << endl; // 0
cout << sizeof(bool) << endl; // 1
输入(cin)
从键盘获取输入。
示例:
cin >> a;
cin >> d;
cin >> ch;
cin >> str;
cin >> flag;
运算符
包含:
- 算术运算符
- 赋值运算符
- 比较运算符
- 逻辑运算符
算术运算符
包括:
+ - * / % ++ --
注意:
- 两个整型相除仍为整型
- 取模 % 只能用在整数之间
- 除数不能为 0
示例:
cout << 10 / 3 << endl;
cout << 10 % 3 << endl;
程序流程结构(Process Control)
C/C++ 支持三大流程结构:
- 顺序结构:代码从上到下依次执行
- 选择结构:根据条件决定执行哪段代码
- 循环结构:满足条件时重复执行某段代码
1. if 语句(选择结构)
单行格式 if
语法:
if (条件) {
执行语句;
}
示例:
int main() {
int score = 0;
cout << "请输入一个分数:" << endl;
cin >> score;
if (score > 600) {
cout << "我考上了一本大学!!!" << endl;
}
system("pause");
return 0;
}
注意
❗ if 条件后 不能加分号
否则会变成“空语句”,导致判断失效。
多行格式 if…else
if (条件) {
...
} else {
...
}
示例:
int score = 0;
cout << "请输入考试分数:" << endl;
cin >> score;
if (score > 600) {
cout << "我考上了一本大学" << endl;
} else {
cout << "我未考上一本大学" << endl;
}
多条件 if…else if…else
if (条件1) {
...
} else if (条件2) {
...
} else if (条件3) {
...
} else {
...
}
示例:
if (score > 600) {
cout << "一本" << endl;
} else if (score > 500) {
cout << "二本" << endl;
} else if (score > 400) {
cout << "三本" << endl;
} else {
cout << "未考上本科" << endl;
}
if 嵌套
示例(包含北大/清华/人大条件):
if (score > 600) {
cout << "我考上了一本大学" << endl;
if (score > 700) {
cout << "我考上了北大" << endl;
} else if (score > 650) {
cout << "我考上了清华" << endl;
} else {
cout << "我考上了人大" << endl;
}
}
2. 三目运算符(?:)
语法:
表达式1 ? 表达式2 : 表达式3
说明:
- 若表达式1为真 → 返回表达式2
- 若表达式1为假 → 返回表达式3
示例:
int a = 10;
int b = 20;
int c = a > b ? a : b;
cout << "c = " << c << endl;
注意
C++ 的三目运算符返回变量本体,可以继续赋值:
(a > b ? a : b) = 100;
3. switch 语句
适用于“多分支固定值判断”。
语法:
switch (表达式) {
case 值1:
语句;
break;
case 值2:
语句;
break;
default:
语句;
break;
}
示例:
int score;
cin >> score;
switch (score) {
case 10:
case 9:
cout << "经典" << endl;
break;
case 8:
cout << "非常好" << endl;
break;
case 7:
case 6:
cout << "一般" << endl;
break;
default:
cout << "烂片" << endl;
break;
}
注意事项
- ❗ switch 表达式只能是整数或字符类型
- ❗ case 不写 break 会继续向下执行(穿透)
- 不能用于判断区间(适合 if)
4. while 循环
语法:
while (条件) {
执行语句;
}
示例:
int num = 0;
while (num < 10) {
cout << num << endl;
num++;
}
注意
必须保证循环内有“跳出条件”,否则会变成死循环。
5. do…while 循环
先执行,再判断。
do {
执行语句;
} while (条件);
示例:
int num = 0;
do {
cout << num << endl;
num++;
} while (num < 10);
6. for 循环
语法:
for (起始; 条件; 末尾) {
执行语句;
}
示例:
for (int i = 0; i < 10; i++) {
cout << i << endl;
}
7. break / continue / goto
break 用途
- 跳出 switch
- 跳出循环
- 跳出最近一层循环(多层嵌套也只能跳最近)
示例:switch 中
switch (num) {
case 1:
cout << "普通" << endl;
break;
...
}
示例:for 中断
for (int i = 0; i < 10; i++) {
if (i == 5) break;
}
continue 用途
跳过“本次循环剩余语句”,继续下一轮循环。
示例:
for (int i = 0; i < 100; i++) {
if (i % 2 == 0) continue;
cout << i << endl;
}
goto(不推荐)
语法:
goto 标记;
...
标记:
示例:
cout << "1" << endl;
goto FLAG;
cout << "2" << endl;
FLAG:
cout << "5" << endl;
数组(Array)
1. 一维数组
三种定义方式:
int score[10];
int score[10] = {100, 90, ...};
int score[] = {100, 90, ...};
注意事项:
- 下标从 0 开始
{}不足时补 0- 数组名不能重新赋值(是常量)
示例:
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 arr[10] = {1,2,3,4,5,6,7,8,9,10};
cout << sizeof(arr) << endl;
cout << sizeof(arr[0]) << endl;
cout << (int)arr << endl;
cout << (int)&arr[0] << endl;
2. 冒泡排序(Bubble Sort)
步骤:
- 比较相邻元素
- 大值交换到后面
- 每趟减少一次比较
- 重复直到无需比较
示例(升序):
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;
}
}
}
3. 二维数组
四种写法:
int arr[2][3];
int arr[2][3] = {{1,2,3},{4,5,6}};
int arr[2][3] = {1,2,3,4,5,6};
int arr[][3] = {1,2,3,4,5,6};
二维数组的大小与地址
cout << sizeof(arr) << endl;
cout << sizeof(arr[0]) << endl;
cout << sizeof(arr[0][0]) << endl;
cout << arr << endl;
cout << arr[0] << endl;
cout << arr[1] << endl;
cout << &arr[0][0] << endl;
函数(Function)
函数的作用是 将重复的代码封装起来,让程序结构更清晰,也方便复用。
一般来说,一个大型程序由多个函数模块组成,每个模块完成一个特定任务。
函数的定义
函数定义由五部分组成:
- 返回值类型
- 函数名
- 参数列表
- 函数体语句
- return 语句
通用语法格式:
返回值类型 函数名(参数列表) {
函数体语句
return 返回值;
}
示例:定义一个加法函数
int add(int num1, int num2)
{
int sum = num1 + num2;
return sum;
}
函数的调用
语法:
函数名(实参);
示例:
int a = 10;
int b = 20;
int sum = add(a, b);
cout << "sum = " << sum << endl;
返回值类型(return)说明
- 若函数有返回值,必须使用
return返回对应类型 - 若函数无返回值(
void),return可以省略
形参 vs 实参
int add(int num1, int num2) // num1、num2 是形参
{
return num1 + num2;
}
add(a, b); // a、b 是实参
注意
❗ 形参是实参的一个拷贝(值传递)
改变形参不会影响实参。
值传递(Value Passing)
示例:
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 = " << a << endl;
cout << "b = " << b << endl;
return 0;
}
结论
值传递中,形参修改 不会 影响实参。
函数的四种常见类型
1. 无参无返
void test01()
{
cout << "this is test01" << endl;
}
2. 有参无返
void test02(int a)
{
cout << "this is test02" << endl;
cout << "a = " << a << endl;
}
3. 无参有返
int test03()
{
cout << "this is test03 " << endl;
return 10;
}
4. 有参有返
int test04(int a, int b)
{
cout << "this is test04 " << endl;
int sum = a + b;
return sum;
}
函数的声明(Declaration)
作用:告诉编译器函数的名称及用法。定义可以写在下面,但必须先声明。
int max(int a, int b);
int max(int a, int b); // 声明可重复
定义只能出现一次:
int max(int a, int b)
{
return a > b ? a : b;
}
函数的“分文件编写”
用于让项目更清晰:
- 声明写在
.h - 定义写在
.cpp - 主函数调用写在
main.cpp
示例
swap.h
#include<iostream>
using namespace std;
void swap(int a, int b); // 函数声明
swap.cpp
#include "swap.h"
void swap(int a, int b)
{
int temp = a;
a = b;
b = temp;
cout << "a = " << a << endl;
cout << "b = " << b << endl;
}
main.cpp
#include "swap.h"
int main() {
int a = 100;
int b = 200;
swap(a, b);
return 0;
}
指针(Pointer)
指针是一种变量,用于存储内存地址。
可以通过指针间接访问内存(解引用)。
指针变量的定义
语法:
数据类型 * 指针名;
示例:
int a = 10;
int *p = &a;
指针与普通变量的区别
| 普通变量 | 指针变量 |
|---|---|
| 存储数据本体 | 存储地址 |
| 访问直接拿数据 | 通过 * 解引用访问 |
示例:
cout << p << endl; // 地址
cout << *p << endl; // 值
指针所占内存空间
所有指针类型大小相同:
- 32 位系统:4 字节
- 64 位系统:8 字节
示例:
cout << sizeof(p) << endl;
cout << sizeof(char*) << endl;
cout << sizeof(double*) << endl;
空指针(NULL 指针)
int *p = NULL;
注意
- 空指针不能访问
- 0~255 号内存是系统保留区,不能访问
会报错:
cout << *p << endl; // 错误
野指针(未初始化的指针)
示例:
int *p = (int *)0x1100;
cout << *p << endl; // 访问非法内存,错误
const 修饰指针的三种情况
1. const 修饰的是“指向的值” → 常量指针
const int *p = &a;
p = &b; // 可以
*p = 100; // 错误
2. const 修饰的是“指针本身” → 指针常量
int * const p = &a;
p = &b; // 错误
*p = 100; // 正确
3. const 修饰“指针和指向的值”
const int * const p = &a;
p = &b; // 错误
*p = 100; // 错误
指针与数组
数组名本身就是指向首地址的“指针常量”。
示例:
int arr[] = {1,2,3};
int *p = arr;
cout << *p << endl; // 访问首元素
for (int i = 0; i < 10; i++) {
cout << *p << endl;
p++;
}
指针与函数(地址传递)
示例:
void swap2(int *p1, int *p2)
{
int temp = *p1;
*p1 = *p2;
*p2 = temp;
}
调用:
swap2(&a, &b); // 会修改实参
结论
想修改实参 → 使用地址传递(指针)
不想修改实参 → 使用值传递
结构体(Struct)
结构体是一种可以存储不同类型数据的自定义数据类型。
结构体定义与使用
语法:
struct 结构体名 {
成员列表
};
结构体变量的三种定义方式:
struct student stu1;
struct student stu2 = {"张三", 18, 100};
struct student { ... } stu3;
示例:
struct student {
string name;
int age;
int score;
} stu3;
student stu1;
stu1.name = "张三";
stu1.age = 18;
stu1.score = 100;
结构体数组
放多个结构体变量:
student arr[3] = {
{"张三", 18, 80},
{"李四", 19, 60},
{"王五", 20, 70}
};
结构体指针
语法:
student stu = {"张三",18,100};
student *p = &stu;
p->score = 80;
注意:-> 是结构体指针访问成员的方式。
结构体嵌套结构体
结构体成员可以是另一个结构体:
struct student {
string name;
int age;
int score;
};
struct teacher {
int id;
string name;
int age;
student stu; // 嵌套
};
结构体作为函数参数
两种方式:
- 值传递(不会修改主函数变量)
- 地址传递(会修改主函数变量)
示例:
void printStudent(student stu)
{
stu.age = 28; // 不影响外部
}
地址方式:
void printStudent2(student *stu)
{
stu->age = 28; // 修改外部
}
结构体 + const
用于防止误修改:
void printStudent(const student *stu)
{
// stu->age = 100; // 错误
cout << stu->name << endl;
}