基础
1 数据
1.1 常量、变量和helloworld的写法
- #define day 7
- const修饰变量
#include <iostream>
using namespace std;
// 常量
#define day 7
int main() {
// 常量
const int a = 14;
// 变量
int b = 16;
cout << "a= " << day << endl;
cout << "Hello的World!" << endl;
return 0;
}
1.2 数据类型
short的取值范围是-32768~32767
1.3 sizeof关键字
#include <iostream>
using namespace std;
// 常量
#define day 7
int main() {
int a = 10;
cout << "short类型所占内存空间为:" << sizeof(short)<<endl;
cout << "int类型所占内存空间为:" << sizeof(a)<<endl;
cout << "long类型所占内存空间为:" << sizeof(long)<<endl;
cout << "long类型所占内存空间为:" << sizeof(long long)<<endl;
return 0;
}
1.4 浮点型
float占用空间4字节,7位有效数字(3.14是三位有效数字)
double占用8字节,15~16位有效数字,默认是双精度
1.5 字符型
需要单引号,占位1
#include <iostream>
using namespace std;
int main() {
char ch = 'a'; // 需要单引号
cout << "ch: "<< ch << endl;
//字符型占用内存大小
cout << "sizeof_char: " << sizeof(ch) << endl; // 1
cout << (int) ch << endl; //强转int可得到ascii编码
}
1.6 科学计算
#include <iostream>
using namespace std;
int main() {
float f2 = 3e2; // 3 * 10 ^ 2 = 300
cout << f2 << endl;
float f3 = 3e-2; // 3 * 0.1 ^ 2 = 0.03
cout << f3 << endl;
return 0;
}
1.7 转义字符
1.8 字符串
#include <iostream>
#include <string>
using namespace std;
int main() {
// 字符串
string str1 = "hello world!";
cout << str1 << endl;
}
1.9 布尔类型bool
#include <iostream>
#include <string>
using namespace std;
int main() {
// 布尔,所占空间都为1
bool flag1 = true; // 1
cout << flag1 << endl;
bool flag2 = false; // 0
cout << flag2 << endl;
}
1.10 数据的输入
输入符号位cin >>
#include <iostream>
#include <string>
using namespace std;
int main() {
// 数据的输入
int a = 0;
cout << "请给整型变量a赋值: " << endl;
cin >> a;
cout << "整型变量a= : " << a << endl;
}
2 运算符
2.1 加减乘除、取模、递增和递减运算符运算符
#include <iostream>
#include <string>
using namespace std;
int main() {
// 运算符
int a = 10;
int b = 3;
//加减乘除
cout << a + b << endl;
cout << a - b << endl;
cout << a * b << endl;
cout << a / b << endl; // 默认为3,整型会去除小数部分
cout << "----------------" << endl;
// 取模运算
int c = 10;
int d = 3;
cout << c % d << endl; //d不能为0,两个小数不能做取模运算
cout << "----------------" << endl;
// 递增、递减运算符
int e = 10;
int f = 10;
++e;
cout << e << endl;
f++;
cout << f << endl;
int g = 15;
int h = ++g * 10;
cout << h << endl; // 160
int i = 15;
int j = i++ * 10;
cout << j << endl; // 160
}
2.2 赋值运算符
加减乘除都可以用,只放一个
#include <iostream>
#include <string>
using namespace std;
int main() {
// 赋值运算符
int a = 10;
a += 1; // a = a + 2
cout << a <<endl;
}
2.3 比较运算符
2.4 逻辑运算符
#include <iostream>
#include <string>
using namespace std;
int main() {
// 逻辑运算符
int a = 10; // 整型为真(1)
cout << "!10: " << !a << endl;
int b = 0; // 0为假(0)
cout << "!0: " << !b << endl;
cout << "0与1: " << (a&&b) << endl;
cout << "0或1: " << (a||b) << endl;
}
3 流程结构
3.1 if语句
#include <iostream>
#include <string>
using namespace std;
int main() {
// if语句
int score = 0;
cout << "请输入一个分数:" << endl;
cin >> score;
cout << "您输入的分数为: " << score << endl;
if (score > 600)
cout << "恭喜您考上了一本大学!" << endl;
else if (score > 500)
cout << "您考上了二本大学!" << endl;
else
cout << "您没有考上二本大学!" << endl;
}
3.2 三目运算符
#include <iostream>
#include <string>
using namespace std;
int main() {
// 三目运算符
int a = 10;
int b = 20;
int c = 0;
c = (a > b ? a : b);
cout << c << endl; // a > b就返回a,否则返回b
(a > b ? a : b) = 100;
cout << a << endl;
cout << b << endl; // a > b就给a赋值100,否则b
}
3.3 switch语句
#include <iostream>
#include <string>
using namespace std;
int main() {
// sitch语句
cout << "请给电影打分: " << endl;
int score = 0;
cin >> score;
cout << "您打的分数为: " << score <<endl;
switch (score) {
case 10:
cout << "您认为是经典电影" << endl;
break;
case 9:
cout << "您认为是经典电影" << endl;
break;
case 8:
cout << "您认为电影还可以" << endl;
break;
}
}
3.4 while循环
#include <iostream>
#include <string>
using namespace std;
int main() {
// while循环
int num = 0;
while(num < 10) {
cout << num << endl;
num += 1;
}
}
3.5 while循环小游戏
#include <iostream>
#include <string>
#include <ctime>
using namespace std;
int main() {
// while循环练习案例:猜数字
// 生成0-100的随机数
srand((unsigned int) time(NULL));
int num = rand() % 100 + 1;
// cout << num << endl;
int val = 0; // 玩家输入的数据
while(1) {
cin >> val;
if (val > num) {
cout << "猜测过大!" << endl;
} else if (val < num) {
cout << "猜测过小!" << endl;
} else {
cout << "猜测成功!" << endl;
break;
}
}
}
3.6 for循环
#include <iostream>
using namespace std;
int main() {
// for循环
for (int i = 0; i < 10; i++) {
cout << i << endl;
}
}
3.7 for循环案例
#include <iostream>
using namespace std;
int main() {
// for循环
for (int i = 1; i <= 100; i++) {
if(i % 10 == 7){
cout << "敲桌子!" << endl;
} else if(i >= 70 && i <80){
cout << "敲桌子!" << endl;
}else if (i % 7 == 0){
cout << "敲桌子!" << endl;
}
else{
cout << i << endl;
}
}
}
3.8 循环案例 - 乘法口诀表
#include <iostream>
using namespace std;
int main() {
// for循环
for (int i = 1; i <= 9; i++){
for (int j = 1; j <= i; j++){
cout << i << "*" << j << " = " << i * j << " ";
}
cout << endl;
}
}
3.9 跳转语句
3.9.1 break语句
跳出当前的循环语句,比continue跳的更外面
3.9.2 continue语句
在循环语句中,跳过本次循环中余下尚未执行的语句,继续执行下一次循环
3.9.3 goto语句
不推荐使用
4 数组
4.1 数组的声明与赋值
#include <iostream>
using namespace std;
int main() {
// 数组
//第一种定义方式
int arr[5];
arr [0] = 10;
arr [1] = 20;
arr [2] = 30;
arr [3] = 40;
arr [4] = 50;
//第二种定义方式
int arr2[5] = {1, 2, 3, 4, 5};
//第三种定义方式
int arr3[] = {1, 2, 3, 4, 5, 6, 7};
for (int i = 0; i < 5; i++) {
cout << arr[i] << endl;
}
}
4.2 一维数组名的
用途:
- 统计数组的长度
- 获取数组内存中的首地址
4.3 求数组最大值
#include <iostream>
using namespace std;
int main() {
// 数组
//第一种定义方式
int max = 0;
int arr[6] = {3, 1, 2, 3, 4, 2};
for (int i = 0; i < 6; i++) {
if (arr[i] > max){
max = arr[i];
}
}
cout << "最大值为:" << max << endl;
}
4.4 数组逆置
#include <iostream>
using namespace std;
int main() {
// 数组
//第一种定义方式
int arr[6] = {3, 1, 2, 3, 4, 2};
int start = 0;
int end = sizeof(arr) / sizeof(int) -1;
while (start < end) {
int tem = arr[start];
arr[start] = arr[end];
arr[end] = tem;
start++;
end--;
}
for (int j = 0; j < 6; j++) {
cout << arr[j];
}
}
4.5 冒泡排序
#include <iostream>
using namespace std;
int main() {
// 数组
//第一种定义方式
int arr[6] = {3, 1, 2, 3, 4, 2};
for (int i = 0; i < 6 - 1; i++) {
for (int j = 0; j < 6 - 1 - i; j++) {
if (arr[j] > arr[j + 1]) {
int tem = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = tem;
}
}
}
for (int k = 0; k < 6; k++) {
cout << arr[k]<< endl;
}
}
4.6 二维数组
#include <iostream>
using namespace std;
int main() {
// 数组
//第一种定义方式
int arr1[2][3];
arr1[0][0] = 1;
arr1[0][1] = 2;
arr1[0][2] = 3;
arr1[1][0] = 4;
arr1[1][1] = 5;
arr1[1][2] = 6;
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++){
cout << arr1[i][j] << endl;
}
}
}
4.7 二维数组案例 - 成绩统计
#include <iostream>
using namespace std;
int main() {
// 数组
//第一种定义方式
int arr1[2][3];
arr1[0][0] = 100;
arr1[0][1] = 100;
arr1[0][2] = 100;
arr1[1][0] = 90;
arr1[1][1] = 50;
arr1[1][2] = 100;
arr1[2][0] = 60;
arr1[2][1] = 70;
arr1[2][2] = 80;
for (int i = 0; i < 3; i++) {
int sum_ = 0;
for (int j = 0; j < 3; j++){
sum_ += arr1[i][j];
}
cout << sum_ << endl;
}
}
5 函数
作用:将一段经常使用的代码封装起来,减少重复代码
5.1 函数的定义和调用
- 返回值类型
- 函数名
- 参数列表
- 函数体语句
- return表达式
#include <iostream>
using namespace std;
int add(int num1, int num2) {
return num1 + num2;
}
int main() {
// 函数
cout << add(1, 2) << endl;
}
5.2 值传递
函数中的值发生了改变,函数外的值没变
#include <iostream>
using namespace std;
void swap(int num1, int num2){
cout << "交换前: "<< endl;
cout << "num1: " << num1 << endl;
cout << "num2: " << num2 << endl;
int temp = num1;
num1 = num2;
num2 = temp;
cout << "交换后: "<< endl;
cout << "num1: " << num1 << endl;
cout << "num2: " << num2 << endl;
}
int main() {
// 值传递
int num1 = 10;
int num2 = 20;
swap(num1, num2);
cout << "实参没变: " << endl;
cout << "num1: " << num1 << endl;
cout << "num2: " << num2 << endl;
}
5.3 函数的声明
- 提前告诉编译器有这个函数
- 可以有多次声明
5.4 函数的分文件编写
步骤:
- 创建后缀名为.h的头文件
- 创建后缀名为.cpp的源文件
- 在头文件中写函数的生命
- 在源文件中写函数的定义
示例:创建swap.cpp和swap.h 都需要在一个目录下
swap.h文件:
#include <iostream>
using namespace std;
// 函数的声明
void swap(int a, int b);
swap.cpp文件:
#include "swap2.h"
#include <iostream>
using namespace std;
//
// Created by 万方名 on 2020-05-23.
//
void swap(int a, int b) {
int temp = a;
a = b;
b = temp;
cout << a<< endl;
cout << b<< endl;
}
主文件:
#include <iostream>
#include "swap2.h"
using namespace std;
int main() {
// 值传递
int num1 = 10;
int num2 = 20;
swap(num1, num2);
cout << "实参没变: " << endl;
cout << "num1: " << num1 << endl;
cout << "num2: " << num2 << endl;
}
6 指针
6.1 指针的基本概念
作用:可以通过指针间接访问内存
- 内存编号从0开始记录,一般用十六进制数字表示
- 可以利用指针变量保存地址
6.2 指针的定义和使用
#include <iostream>
#include "swap2.h"
using namespace std;
int main() {
// 指针
// 定义变量
int a = 10;
// 定义指针
int *p;
// 让指针记录变量a的地址
p = &a;
cout << p << endl;
// 解指针
cout << *p <<endl;
// 通过指针更改变量的值
*p = 1000;
cout << "a: " << a << endl;
cout << "*p: " << *p << endl;
}
6.3 指针所占内存空间
- 32位系统下占四个字节
- 64位系统下占八个字节
#include <iostream>
using namespace std;
int main() {
// 指针
// 定义变量
int a = 10;
int *p = &a;
cout << *p << endl;
cout << sizeof(p) << endl;
cout << sizeof(char *) << endl;
cout << sizeof(float *) << endl;
cout << sizeof(double *) << endl;
}
6.4 空指针和野指针
6.4.1 空指针:指针变量指向内存中编号为0的空间
- 用途:初始化指针变量
- 注意:空指针指向的内存是不可访问的
#include <iostream>
using namespace std;
int main() {
// 空指针
int *p = NULL;
// 不可访问
cout << *p << endl;
}
6.4.2 野指针:指针变量指向非法的内存空间
在程序中尽量避免出现野指针
#include <iostream>
using namespace std;
int main() {
// 野指针
int *p = (int *) 0x1100;
// 报错
cout << *p << endl;
}
6.5 const修饰指针
三种情况:
- const修饰指针 - 常量指针
- const修饰常量 - 指针常量
- const即修饰指针又修饰常量
#include <iostream>
using namespace std;
int main() {
// 1.常量指针
int a = 10;
int b = 20;
// 定义常量指针
const int *p = &a;
// *p = 20; // 报错,常量指针的值不可修改
p = &b; // 不报错,指针可以重新指向b
cout << *p << endl;
// 2.指针常量
int c = 30;
int d = 40;
// 定义指针常量
int *const p2 = &c;
*p2 = 50; // 不报错,指针的值可以修改
// p2 = &d; // 报错,指针不可更改指向
cout << *p2 << endl;
//3. const修饰指针和指针常量时不可更改值和指向
}
6.6 指针和数组
作用:利用指针访问数组元素
#include <iostream>
using namespace std;
int main() {
// 指针和数组
int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int *p = arr;
cout << *p << endl; // 返回数组的第一个元素
// 利用指针遍历数组
for (int i = 0; i < 10; i++) {
cout << *p << endl;
p++;
}
}
6.7 指针和函数
作用:利用指针作函数参数,可以修改实参的值
#include <iostream>
using namespace std;
void swap4(int a, int b) {
cout << "值传递"<< endl;
int temp = a;
a = b;
b = temp;
}
void swap5(int *a, int *b) {
cout << "地址传递"<< endl;
int temp2 = *a;
*a = *b;
*b = temp2;
}
int main() {
// 指针和函数
//1. 值传递
int a = 10;
int b = 20;
swap4(a, b);
cout << a<< endl;
cout << b<< endl;
cout << "------分界线------"<< endl;
//2. 地址传递
swap5(&a, &b);
cout << a<< endl;
cout << b<< endl;
}
6.8 指针、数组和函数的案例
案例描述:封装一个函数,利用冒泡排序,实现对整型数组的升序排序
#include <iostream>
using namespace std;
void bubble_sort(int *arr, int len){
for (int i = 0; i < len - 1; i++) {
for (int j = 0; j < len - 1 - i; j++) {
if (arr[j] > arr[j + 1]) {
int tem = arr[j];
arr[j] = arr[j+1];
arr[j+1] = tem;
}
}
}
}
int main() {
// 定义数组
int arr[] = {1, 2, 32, 3, 5, 6, 23};
bubble_sort(arr, sizeof(arr) / sizeof(arr[0]));
for (int i = 0; i < sizeof(arr) / sizeof(arr[0]);i++) {
cout << arr[i] << endl;
}
}
进阶
7 结构体
结构体属于用户自定义的数据类型,允许用户存储不同的数据类型
7.1 结构体的定义与使用
通过结构体创建变量的方式有三种:
- struct结构体名 变量名
- struct结构体名 变量名 = {成员1值,成员2值…}
- 定义结构体时顺便创建变量
#include <iostream>
using namespace std;
int main() {
//结构体
struct student {
string name; // 姓名
int age; // 年龄
int score; // 分数
};
student a = student();
a.age = 10;
cout << a.age << endl;
}
7.2 结构体数组
作用:将自定义的结构体放入到数组中方便维护
#include <iostream>
using namespace std;
struct student {
string name; // 姓名
int age; // 年龄
int score; // 分数
};
int main() {
//结构体
student arr[3] = {
{"张三", 18, 60},
{
"李四", 20, 70
}
};
for (int i = 0; i < 3; i++) {
cout << "姓名:" << arr[i].name
<< "年龄: " << arr[i].age
<< "分数: " << arr[i].score << endl;
}
}
7.3 结构体指针
作用:通过指针访问结构体中的成员
#include <iostream>
using namespace std;
struct student {
string name; // 姓名
int age; // 年龄
int score; // 分数
};
int main() {
//结构体指针
student stu = {"张三", 18, 80};
student *p = &stu;
cout << p->score<< endl;
}
7.4 结构体嵌套结构体
作用:结构体中的成员可以是另一个结构体
#include <iostream>
using namespace std;
struct student {
// 成员列表
string name; // 姓名
int age; // 年龄
int score; // 分数
};
struct teacher {
// 成员列表
int id;
string name; // 姓名
int age; // 年龄
struct student stu; // 辅导的学生
};
int main() {
//结构体嵌套
teacher t;
t.id = 1;
t.name = "老万";
t.age = 50;
t.stu.name = "小万";
t.stu.age = 18;
t.stu.score = 80;
cout << "学生的年龄:" << t.stu.age << endl;
}
7.5 结构体做函数参数
将结构体作为参数向函数中传递
传递方式:
- 值传递
- 地址传递
#include <iostream>
using namespace std;
struct student {
// 成员列表
string name; // 姓名
int age; // 年龄
int score; // 分数
};
// 值传递
void print_stu(student stu){
stu.age = 28;
cout << stu.name << endl;
cout << stu.age << endl;
}
// 地址传递
void print_stu2(student *stu){
stu->age = 28;
cout << stu->name << endl;
cout << stu->age << endl;
}
int main() {
//结构体作为函数参数
student t = {"张三", 20, 80};
print_stu(t);
cout << t.age << endl; // 值传递,函数外没有发生改变
cout << "------分界线------" << endl; // 值传递,函数外没有发生改变
student t2 = {"李四", 30, 100};
print_stu2(&t2);
cout << t2.age << endl; // 地址传递,函数外发生改变
}
7.6 结构体中的const
作用:用const来防止误操作
void print_stu(const student *stu){
}
这样在函数中就不能修改stu的值了
7.7 结构体案例
#include <iostream>
using namespace std;
struct Hero {
string name;
int age;
string sex;
};
int main() {
//结构体案例
struct Hero hero_arr[5] = {
{"刘备", 23, "男"},
{"张飞", 20, "男"},
{"关羽", 22, "男"},
{"赵云", 21, "男"},
{"貂蝉", 19, "女"},
};
int len_hero_arr = sizeof(hero_arr) / sizeof(Hero);
cout << len_hero_arr << endl;
for (int i = 0; i < len_hero_arr; i++) {
for (int j = 0; j < len_hero_arr - i; j++) {
if (hero_arr[j].age <= hero_arr[j + 1].age) {
Hero tem = hero_arr[j];
hero_arr[j] = hero_arr[j + 1];
hero_arr[j + 1] = tem;
}
}
}
for (int i = 0; i < len_hero_arr; i++) {
cout << hero_arr[i].name << endl;
}
}
8 项目一 - 通讯录管理系统
//
// Created by 万方名 on 2020-05-23.
//
#include <iostream>
using namespace std;
#include <string>
#define MAX 1000
// 菜单界面
void show_menu(){
cout << "*************************" << endl;
cout << "***** 1、添加联系人 *****" << endl;
cout << "***** 2、显示联系人 *****" << endl;
cout << "***** 3、删除联系人 *****" << endl;
cout << "***** 4、查找联系人 *****" << endl;
cout << "***** 5、修改联系人 *****" << endl;
cout << "***** 6、清空联系人 *****" << endl;
cout << "***** 0、退出通讯录 *****" << endl;
cout << "*************************" << endl;
}
struct Preson{
// 姓名
string m_name;
//性别 1 男 2 女
int m_sex;
// 年龄
int m_age;
// 电话
string m_phone;
// 住址
string m_addr;
};
// 通讯录结构体
struct addresss_books{
struct Preson person_arr[MAX];
int m_size;
};
// 添加联系人函数
void add_person(addresss_books *abs) {
// 判断通讯录是否已满
if (abs->m_size == MAX) {
cout << "通讯录已满,无法添加联系人" << endl;
return;
} else {
// 添加具体联系人
// 姓名
string name;
cout << "请输入姓名: "<< endl;
cin >> name;
abs->person_arr[abs->m_size].m_name = name;
// 性别
int sex = 0;
cout << "请输入性别: "<< endl;
cout << "1 --- 男"<< endl;
cout << "2 --- 女 "<< endl;
cin >> sex;
while(true){
if (sex == 1 || sex == 2) {
abs->person_arr[abs->m_size].m_sex = sex;
break;
} else {
cout << "请重新输入: "<< endl;
cout << "1 --- 男"<< endl;
cout << "2 --- 女 "<< endl;
cin >> sex;
}
}
// 年龄
int age;
cout << "请输入年龄: "<< endl;
cin >> age;
abs->person_arr[abs->m_size].m_age = age;
// 电话
string phone;
cout << "请输入电话: "<< endl;
cin >> phone;
abs->person_arr[abs->m_size].m_phone = phone;
// 住址
string addr;
cout << "请输入家庭住址: "<< endl;
cin >> addr;
abs->person_arr[abs->m_size].m_addr = addr;
// 更新通讯录人数
abs->m_size++;
cout << "添加成功!" << endl;
system("clear");
}
}
// 2、显示所有联系人
void show_person(addresss_books *abs){
// 判断通讯录是否为空
if (abs->m_size == 0) {
cout << "当前通讯录为空!" << endl;
} else {
for (int i = 0; i < abs->m_size; i++) {
cout << "姓名: " << abs->person_arr[i].m_name << endl;
cout << "性别: " << (abs->person_arr[i].m_sex == 1 ? "男" : "女") << endl;
cout << "年龄: " << abs->person_arr[i].m_age << endl;
cout << "电话: " << abs->person_arr[i].m_phone << endl;
cout << "住址: " << abs->person_arr[i].m_addr << endl;
cout << "*************************" << endl;
}
}
system("clear");
}
int is_exist(addresss_books *abs, string name){
for (int i = 0; i < abs->m_size; i++) {
if (abs->person_arr[i].m_name == name) {
return i;
}
}
return -1; // 如果没有找到,返回-1
}
void del_preson(addresss_books *abs){
cout << "请输入要删除的联系人姓名: " << endl;
string name;
cin >> name;
int ret = is_exist(abs, name);
if (ret != -1){
for (int i = 0; i < abs->m_size; ++i) {
// 数据前移
abs->person_arr[i] = abs->person_arr[i + 1];
}
abs->m_size--;
cout << "联系人删除成功!" << endl;
}
}
void find_person(addresss_books *abs){
cout << "请输入您要查找的联系人: " << endl;
string name;
cin >> name;
int ret = is_exist(abs, name);
if (ret != -1) {
cout << "姓名:" << abs->person_arr[ret].m_name << "\t";
cout << "性别:" << abs->person_arr[ret].m_sex << "\t";
cout << "年龄:" << abs->person_arr[ret].m_age << "\t";
cout << "电话:" << abs->person_arr[ret].m_phone << "\t";
cout << "住址:" << abs->person_arr[ret].m_addr << "\t";
}else{
cout << "查无此人!" << endl;
}
system("clear");
}
void modify_person(addresss_books *abs){
cout << "请输入您要修改的联系人:" <<endl;
string name;
cin >> name;
int ret = is_exist(abs, name);
if (ret != -1) {
string name;
cin >> name;
cout << "请输入姓名: " << endl;
abs->person_arr[ret].m_name = name;
int sex;
cin >> sex;
cout << "请输入性别(1 -- 男 2 -- 女): " << endl;
abs->person_arr[ret].m_sex = sex;
int age;
cin >> age;
cout << "请输入年龄: " << endl;
abs->person_arr[ret].m_age = age;
string phone;
cin >> phone;
cout << "请输入电话: " << endl;
abs->person_arr[ret].m_phone = phone;
string addr;
cin >> phone;
cout << "请输入地址: " << endl;
abs->person_arr[ret].m_addr = addr;
cout << "修改成功!" << endl;
} else {
cout << "查无此人!" << endl;
}
}
void clean_person(addresss_books *abs){
abs->m_size = 0;
cout << "联系人已清空!" << endl;
system("clear");
}
int main(){
// 创建通讯录结构体变量
addresss_books abs;
abs.m_size = 0;
int select = 0;
while (true) {
show_menu();
cin >> select;
switch (select) {
case 1: // 1、添加联系人
add_person(&abs);
break;
case 2: // 显示联系人
show_person(&abs);
break;
case 3: // 删除联系人
del_preson(&abs);
break;
case 4: // 查找联系人
find_person(&abs);
break;
case 5: // 修改联系人
modify_person(&abs);
break;
case 6:
break;
case 0: // 退出通讯录
cout << "欢迎下次使用" << endl;
return 0;
break;
default:
break;
}
}
}
C++核心编程
9 内存模型
9.1 内存四区 - 代码区
作用:存放函数体的二进制代码,由操作系统进行管理
程序运行前:
- 存放CPU执行的机器指令
- 代码区是共享的,共享的目的是对于频繁被执行的程序只需要在内存中有一份代码即可
- 代码区是只读的,原因是防止程序意外地修改了它的指令
9.2 内存四区 - 全局区
作用:存放全局变量和静态变量以及常量
程序运行前:
- 该区域的数据在程序结束后由操作系统释放
#include <iostream>
using namespace std;
// 全局变量
int g_a = 10;
int g_b = 10;
int main() {
int a = 10;
int b = 10;
cout << "局部变量a的地址:" << &a << endl;
cout << "局部变量b的地址:" << &b << endl;
cout << "全局变量g_a的地址:" << &g_a << endl;
cout << "全局变量g_b的地址:" << &g_b << endl;
}
静态变量:在普通变量前加static
9.3 内存四区 - 栈区
作用:由编译器自动分配和释放,存放函数的参数值、局部变量等
注意事项:不要反悔局部变量的地址,栈区开辟的数据由编译器自动释放
9.4 内存四区 - 堆区
作用:由程序员分配和释放,若程序员不释放,程序结束时由操作系统回收
在C++中主要利用new在堆区开辟内存。
#include <iostream>
using namespace std;
int * func(){
// 利用new关键字,可以将数据开辟到堆区
int *p = new int(10);
return p;
}
int main() {
// 在堆区开辟数据
int *p = func();
cout << *p << endl;
}
9.5 new操作符
- C++利用new操作符在堆区开辟数据
- 堆区开辟的空间由程序员手动释放,释放利用delete操作符
- 利用new创建的数据,会返回概述就对应的类型的指针
#include <iostream>
using namespace std;
int * func(){
// 利用new关键字,可以将数据开辟到堆区
int *p = new int(10);
return p;
}
void test02(){
int *arr = new int[10];
delete []arr;
}
int main() {
// 1、new的基本语法
int *p = func();
cout << *p << endl;
delete p; // 释放堆区的数据
// 2、在堆区利用new开辟数组
test02();
}
10 引用
10.1 引用示例
作用:给变量起别名
#include <iostream>
using namespace std;
int main() {
// 引用
int a = 10;
int &b = a;
cout << "a = " << a <<endl;
cout << "b = " << b <<endl;
a = 20;
cout << "a = " << a <<endl;
cout << "b = " << b <<endl;
}
10.2 引用注意事项
- 引用必须初始化
- 引用在初始化后不可改变
10.3 引用做函数参数
作用:函数传参时,可以利用引用的技术让形参修饰实参
优点:可以简化指针修改实参