普通算法题不太涉及的C++知识点(二)
1. 内存分区模型:
C++将内存大致分为4个区域:
代码区:存放函数体的二进制代码,由操作系统进行管理;
全局区:存放全局变量,静态变量和常量;
栈区:存放函数的参数值,局部变量,由编译器自动分配释放;
堆区:由程序员分配和释放,若程序员不释放,程序结束时由操作系统回收。
1.1 程序运行前:
程序编译后,未执行时,该程序分为两个区域:
1.1.1 代码区:
存放CPU执行的机器指令。
代码区是共享,只读的。
1.1.2 全局区
1.2 程序运行后:
1.2.1 栈区:
注意:不要返回局部变量的地址。
int *func(){
int a = 10;
return &a; //这样是可能拿不到10的;
}
1.2.2 堆区:
在C++中主要利用new在堆区开辟内存。
int *func(){
int *p = new int(10);
return p; //可以拿到10;
}
手动释放堆区内存:
delete p;
在堆区开辟数组:
int *arr = new int[10];
int a[] = new int[10];
释放堆区数组:
delete[] arr;
2. 引用:
2.1 引用基本用法:
作用:给变量起别名。
int a = 10;
int &b = a;
b = 20;
cout << a << endl; //输出20;
注意:引用必须初始化,且初始化后不能更改,但是可以赋值。
int a = 10;
int &b = a;
int c = 20;
b = c; //这是赋值操作,不是更改引用;
2.2 引用做函数参数:
可以通过形参修改实参。
void func(int &a, int &b){
int temp = a;
a = b;
b = temp;
}
int main(){
int a = 10;
int b = 20;
func(a, b);
return 0;
}
2.3 引用做函数返回值:
不要返回局部变量的引用:
int& func(){
int a = 10;
return &a;
}
int main(){
int &ref = func();
cout << ref << endl; //可能无法输出正确结果10;
return 0;
}
函数的调用可以作为左值:
int& func(){
static int a = 10;
return &a;
}
int main(){
int &ref = func();
cout << ref << endl; //输出10;
func() = 1000;
cout << ref << endl; //输出1000;
return 0;
}
2.4 引用的本质:
引用的本质是一个指针常量。
2.5 常量引用:
常量引用主要用来修饰形参,防止误操作。
void show(const int &a){ //这样能使show函数内的修改操作无效;
cout << a << endl;
}
int main(){
//int &ref = 10; //错误;
const int &ref = 10; //正确;
//ref = 20; //错误,使用const修饰后,不能修改;
int a = 100;
show(a);
return 0;
}
3. 函数提高:
3.1 函数默认值:
int func(int a, int b = 20, int c = 30){
return a + b + c;
}
int main(){
cout << func(10) << endl; //输出60;
cout << func(10, 30) << endl; //输出70;
return 0;
}
函数声明和函数实现只能有一个有默认参数。
3.2 函数占位参数:
C++函数的形参列表里可以有占位参数,调用函数时必须填补该位置。
int func(int a, int){
return a;
}
int main(){
cout << func(10, 30) << endl; //
return 0;
}
占位参数可以有默认参数:
int func(int a, int = 10){
return a;
}
int main(){
cout << func(10) << endl; //
return 0;
}
3.3 函数重载:
3.3.1 基本操作:
函数重载满足条件:
- 同一个作用域下;
- 函数名相同;
- 函数的参数类型或个数或顺序不同。
void func(){
cout << 10 << endl;
}
void func(int a){
cout << 20 << endl;
}
void func(double a){
cout << 30 << endl;
}
void func(int a, double b){
cout << 40 << endl;
}
void func(double a, int b){
cout << 50 << endl;
}
int main(){
func(); //调用func();
func(10); //调用func(int a);
func(3.14); //调用func(double a);
func(10, 3.14);
func(3.14, 10);
return 0;
}
3.3.2 引用作为重载条件:
void func(int &a){
cout << 10 << endl;
}
void func(const int &a){
cout << 20 << endl;
}
int main(){
int a = 10;
func(a); //输出10;
func(10); //输出20,因为int &a=10不合法,const int &a=10合法;
return 0;
}
3.3.3 函数重载遇到默认参数:
void func(int a, int b=10){
cout << 10 << endl;
}
void func(int a){
cout << 20 << endl;
}
int main(){
func(10); //出现歧义,出错;
return 0;
}