0
点赞
收藏
分享

微信扫一扫

如何优雅地停掉线程?

雨鸣静声 2023-05-06 阅读 33
c++

👂 夜车(电影《恋爱通告》片尾曲) - 曾轶可 - 单曲 - 网易云音乐

纯记录,,,同时方便以后检索 / 回顾,结合gpt和百度进行注释 / 解释...

目录

🏆内存四区--堆区

🏆new操作符

🏆引用

🏆函数高级


🏆内存四区--堆区

#include<iostream>
using namespace std;

int * func() //函数返回一个指向int类型变量的指针
{
//利用new关键字将数据开辟到堆区
//此处指针也是局部变量,放在栈上,指针保存的数据放在堆区
//堆区上的地址编号, 用栈区上的指针保存
int * p = new int(10); //10是初始值
return p;
}

int main()
{
//在堆区开辟数据
int *p = func();
cout<<*p<<endl;
cout<<*p<<endl;
cout<<*p<<endl;

system("pause");
return 0;
}
10
10
10
请按任意键继续. . .

关于第4行,int * func()中的 *

1,堆区数据由程序员管理和释放

2,堆区数据利用new关键字开辟内存

🏆new操作符

#include<iostream>
using namespace std;

//1, new的基本语法
int * func()
{
//在堆区创建整型数据
//new返回的是 该数据类型的指针
int * p = new int(10);
return p;
}

void test01()
{
int *p = func();
cout<<*p<<endl;
cout<<*p<<endl;
cout<<*p<<endl;
//堆区数据 由程序员管理开辟,程序员管理释放
//如果想释放堆区的数据,利用关键字delete
delete p;

//cout<<*p<<endl; //内存已释放, 再次访问非法, 报错
}

//2, 在堆区利用new开辟数组
void test02()
{
int * arr = new int[10]; //创建一个包含10个元素的数组
for(int i = 0; i < 10; ++i)
arr[i] = i + 100; //给10个元素赋值100~109
for(int i = 0; i < 10; ++i)
cout<<arr[i]<<endl;
//释放堆区数组
delete[] arr; //释放数组时, 要加[]
}

int main()
{
test01();
cout<<endl;
test02();

//1, new的基本语法

//2, 在堆区利用new开辟数组

system("pause");
return 0;
}
10
10
10

100
101
102
103
104
105
106
107
108
109
请按任意键继续. . .
new 数据类型(初始值)
new 数据类型[数组长度]

由于程序员有责任管理堆区的内存,因此在使用 new 语句时,要注意及时释放分配出来的内存避免出现内存泄漏等问题

🏆引用

引用(Reference)是 C++ 语言中的一种数据类型,它提供了一种简单而又安全地通过别名访问变量的方式。引用的本质就是某个变量的一个别名,并不是一个新的存储空间,它和原变量使用同一块存储空间。

三种用法

1,定义引用变量

int a = 10;
int a;

2,引用作为函数参数

void swap(int& x, int& y) {
int temp = x;
x = y;
y = temp;
}

3,引用作为函数返回值

int& get_max(int& x, int& y) {
return x > y ? x : y;
}

引用基本语法 

#include<iostream>
using namespace std;

int main()
{
//引用基本语法
//数据类型 原名
int a = 10;
int a;
cout<<a<<endl;
cout<<b<<endl;

b = 100;
cout<<a<<" "<<b<<endl;

system("pause");
return 0;
}
10
10
100 100
请按任意键继续. . .

引用注意事项

#include<iostream>
using namespace std;



int main()
{
//引用必须初始化
int a = 10;
//int //错误, 必须初始化
int a; //初始化
//引用初始化后, 不可更改
int c = 20;
b = c; //此处赋值操作, 不是改引用
cout<<a<<endl;
cout<<b<<endl;
cout<<c<<endl;

system("pause");
return 0;
}
20
20
20
请按任意键继续. . .

 引用作函数参数

#include<iostream>
using namespace std;

//交换函数
//1, 值传递
void mySwap01(int a, int b)
{
int temp = a;
a = b;
b = temp;
}
//2, 地址传递
void mySwap02(int *a, int *b) //指针接受传入的地址
{
int temp = *a; //*解引用
*a = *b;
*b = temp;
}
//3, 引用传递
int mySwap03(int &a, int &b) //&a里的a是主函数a的别名, 地址相同
{
int temp = a;
a = b;
b = temp;
}

int main()
{
int a = 10, b = 20;
mySwap01(a, b);
cout<<"值传递: "<<a<<" "<<b<<endl;
mySwap02( //传入地址
cout<<"地址传递: "<<a<<" "<<b<<endl;

a = 10, b = 20;
//引用传递, 形参也会修饰实参
mySwap03(a, b);
cout<<"引用传递: "<<a<<" "<<b<<endl;

system("pause");
return 0;
}
值传递: 10 20
地址传递: 20 10
引用传递: 20 10
请按任意键继续. . .

引用作函数返回值

#include<iostream>
using namespace std;

//引用做函数的返回值
//1, 不要返回局部变量的引用
int& test01()
{
int a = 10; //局部变量存放在四区中的 栈区
return a;
}

//2, 函数的调用可以作为左值
int& test02()
{
static int a = 10; //static变量存放在 全局区
return a;
}

int main()
{
//函数结束后, 变量a的栈空间被释放
int test01();
//cout<<r<<endl; //无法输出

//test02()是函数调用
int  test02();
cout<<r2<<endl;
cout<<r2<<endl;
test02() = 1000; //函数调用作为左值, 将1000赋值给r2
cout<<r2<<endl;
cout<<r2<<endl;

system("pause");
return 0;
}
10
10
1000
1000
请按任意键继续. . .

引用本质

#include<iostream>
using namespace std;

//发现是引用, 转换为int* const r =
//引用本质就是一个指针常量
//引用一旦初始化后, 就不可改变
void func(int& r)
{
r = 100; //r是引用, 转换为*r = 100;
}

int main()
{
int a = 10;
//自动转换为int* const r =
//指针常量, 指针指向不可改变, 也就是引用不可更改
int a;
cout<<r<<endl;

r = 20; //内部发现r是引用, 自动转换为*ref = 20;
cout<<r<<endl;

func(a);
cout<<r<<endl;

system("pause");
return 0;
}
10
20
100
请按任意键继续. . .

常量引用

#include<iostream>
using namespace std;

//打印数据
void showValue(const int &val) //引用传递
{ //const修饰形参, 防止误操作
//val = 1000;
cout<<"val = "<<val<<endl;
}

int main()
{
//常量引用
//使用场景: 修饰形参, 防止误操作

//int a = 10;
//加上const后 编译器将代码修改为 int temp = 10; const int temp;
//const int 10; //引用必须是合法的内存空间, 所以这行错误
//r = 20; //加入const后变为只读, 不可修改

int a = 100;
showValue(a);

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

system("pause");
return 0;
}
val = 100
a = 100
请按任意键继续. . .

🏆函数高级

函数默认参数

#include<iostream>
using namespace std;

//函数默认参数

//如果优先传入的数据, 没有就默认值
//语法:返回值类型 函数名 (形参 = 默认值)
int func(int a, int b = 20, int c = 30)
{
return a + b + c;
}
//注意事项

//1,某个位置已经有了默认参数, 从这个位置往后, 从左到右都必须有默认值
//int func2(int a, int b = 10, int c)
//{
//return a + b + c;
//}

//2, 如果函数声明有了默认参数, 函数的实现就不能有默认参数
//声明和实现, 只能有一个有默认参数
int func2(int a = 10, int b = 10); //声明

int func2(int a, int b) //实现
{
return a + b;
}

int main()
{
cout<<func(10, 30)<<endl;

cout<<func2(10, 10)<<endl;

system("pause");
return 0;
}
70
20
请按任意键继续. . .

函数的占位参数

#include<iostream>
using namespace std;

//占位参数
//返回值类型 函数名 (数据类型) {}

//在这个函数体中, 占位参数用不到, 传进去用不上
//占位参数 还可以有默认参数
void func(int a, int = 10)
{
cout<<"this is func"<<endl;
}

int main()
{
//

func(10);

system("pause");
return 0;
}

函数重载 -- 基本语法

条件

注意:函数返回值,不可作为函数重载条件 

比如int func() {}和void func() {}不能作为重载条件,只是返回值不同

#include<iostream>
using namespace std;

//函数重载
//可以让函数名相同, 提高复用性

//函数重载满足条件
//1,同一作用域,比如都是全局函数
//2,函数名称相同
//3,函数参数类型,个数,顺序不同

void func()
{
cout<<"func调用----"<<endl;
}

void func(int a)
{
cout<<"func调用3"<<endl;
}

void func(double a)
{
cout<<"func调用3.14"<<endl;
}

void func(int a, double b)
{
cout<<"func调用 int a, double b"<<endl;
}

void func(double a, int b)
{
cout<<"func调用 double a, int b"<<endl;
}

int main()
{
func();
func(10.0);
func(10 ,3.14);

system("pause");
return 0;
}
func调用----
func调用3.14
func调用 int a, double b
请按任意键继续. . .

函数重载 -- 注意事项

#include<iostream>
using namespace std;

//函数重载注意事项
//1. 引用作为重载的条件
void func(int &a) //引用传递, int 10;错误,但常量引用可以
{
cout<<"fun(int <<endl;
}

void func(const int &a) //const int 10;合法, 常量引用
{
cout<<"
fun(const int <<endl;
}

//2.函数重载碰到默认参数
void func2(int a, int b = 10)
{
cout<<"func2(int a, int b = 10)的调用"<<endl;
}

void func2(int a)
{
cout<<"func2(int a)的调用"<<endl;
}

int main()
{
int a = 10; //a是变量, 可读可写
func(a); //调用无const

func(10); //调用有const

//func2(10); //函数重载碰到默认参数, 出现二义性, 歧义报错
func2(10, 20);

system("pause");
return 0;
}
fun(int &a) 调用
fun(const int &a) 调用
func2(int a, int b = 10)的调用
请按任意键继续. . .

终于到第99个视频了,而同班同学半个月前就第120个视频了,慢了40个视频大概,,

举报

相关推荐

0 条评论