C++基础
C++关键字
C++总共有63个关键字,C语言有32个关键字
如下是C++的关键字,但是不对它进行细讲,以后再讲解
命名空间
在C/C++中,变量、函数和后面要学到的类都是大量存在的,这些变量、函数和类的名称将都存在于全局作用域中,可能会导致很多冲突。使用命名空间的目的是对标识符的名称进行本地化,以避免命名冲突或名字污染,namespace关键字的出现就是针对这种问题的
命名空间定义
命名冲突问题
1.我们自己定义的变量,函数可能和库里面重名冲突
2.进入公司项目组后,做的项目通常比较大。多人协作,两个同事写的代码,命名冲突
C语言没有好的解决办法
CPP提出一个新语法,命名空间
#include <stdio.h>
#include <stdlib.h>
//定义了一个叫yelei的命名空间 -- 域
namespace yelei
{
//它们还是全局变量,放在静态区中
int rand = 0;
int a = 1;
}
int a = 0;
int main()
{
printf("hello world\n");
//先局部域后全局域
printf("%d\n", rand);
printf("%d\n", yelei::rand);
int a = 1;
printf("%d\n", a);
printf("%d\n", ::a);//在全局域中找
return 0;
}
运行结果如下:
普通的命名空间
namespace yelei
{
//命名空间可以定义变量/函数/类型
int rand = 10;
int Add(int left, int right)
{
return left + right;
}
struct Node
{
struct Node* next;
int val;
};
}
命名空间可以嵌套
namespace N1
{
int a;
int b;
int Add(int left, int right)
{
return left + right;
}
namespace N2
{
int c;
int d;
int Sub(int left, int right)
{
return left - right;
}
}
}
同一个工程中允许存在多个相同名称的命名空间,编译器最后会合成同一个命名空间中
List.h
#pragma once
namespace yelei
{
static int rand;
struct ListNode
{
//...
};
void ListInit();
void ListPushBack(struct ListNode* phead, int x);
}
List.cpp
#include "List.h"
namespace yelei
{
void ListInit()
{
//....
}
void ListPushBack(struct ListNode* phead, int x)
{
//....
}
}
Test.cpp
#include <stdio.h>
#include <stdlib.h>
#include "List.h"
namespace bit
{
int rand;
struct Node
{
struct Node* next;
int val;
};
int Add(int left, int right)
{
return left + right;
}
}
namespace N1
{
int a;
int b;
int Add(int left, int right)
{
return left + right;
}
namespace N2
{
int c;
int d;
int Sub(int left, int right)
{
return left - right;
}
}
}
int main()
{
bit::rand = 10;
struct bit::Node node;
bit::Add(1, 2);
N1::Add(1, 2);
N1::N2::Sub(3, 4);
struct yelei::ListNode LTN;
yelei::ListInit();
yelei::ListPushBack(<N, 2);
return 0;
}
命名空间的使用
加命名空间名称及作用域限定符
int main()
{
printf("%d\n", N::a);
return 0;
}
使用using将命名空间中成员引入
using N::b;//单独展开某一个,其他不展开
int main()
{
printf("%d\n", N::a);
printf("%d\n", b);
return 0;
}
使用using namespace命名空间名称引入
using namespace N;//将整个命名空间展开 -- 展开到全局了
//这种慎用,因为变成全局域,会和库里面重名冲突
int main()
{
printf("%d\n", a);
printf("%d\n", b);
Add(3, 4);
return 0;
}
C++输入&输出
1.<< 流插入操作符
2.>> 流提取操作符
#include <iostream>
using namespace std;//C++库的实现定义在一个叫std的命名空间中
//using std::cout;
//using std::endl;
//using std::cin;
int main()
{
cout << "hello world << endl";
cout << "hello world\n";
int i = 10;
double d = 1.11;
//自动识别类型
cout << i << " " << d << endl;
cin >> i >> d;
cout << i << " " << d << endl;
return 0;
}
缺省参数
缺省参数概念
缺省参数是声明或定义函数时为函数的参数指定一个默认值。在调用该函数时,如果没有指定实参则采用该默认值,否则使用指定的实参
#include <iostream>
void func(int a = 0)
{
cout << a << endl;
}
int main()
{
func();
func(1);
}
缺省参数分类
全缺省参数
#include <iostream>
void Func(int a = 10, int b = 20, int c = 30)
{
cout << "a = " << a << endl;
cout << "b = " << b << endl;
cout << "c = " << c << endl;
}
int main()
{
Func();
Func(1);
Func(1, 2);
Func(1, 2, 3);
return 0;
}
半缺省参数 – 缺省部分参数 – 必须从右往左缺省,必须连续缺省
#include <iostream>
//void Func(int a, int b = 20, int c = 30)
void Func(int a, int b, int c = 30)
{
cout << "a = " << a << endl;
cout << "b = " << b << endl;
cout << "c = " << c << endl;
}
int main()
{
Func(10);
Func(10, 20);
Func(10, 20, 40);
return 0;
}
注意:1.缺省参数不能在函数声明和定义中同时出现,要么在声明,要么在定义,推荐写在声明
2.缺省值必须是常量或者全局变量
3.C语言不支持缺省参数(编译器不支持)
Stack.h
#pragma once
#include <stdlib.h>
struct Stack
{
int* a;
int top;
int capacity;
};
void StackInit(struct Stack* ps, int capacity = 4);//缺省值写在声明中
void StackPush(struct Stack* ps, int x);
Stack.c
#include "Stack.h"
void StackInit(struct Stack* ps, int capacity)
{
ps->a = (int*)malloc(sizeof(int) * capacity);
ps->top = 0;
ps->capacity = capacity;
}
void StackPush(struct Stack* ps, int x)
{
//............
}
Test.c
#include "Stack.h"
struct Stack
{
int* a;
int top;
int capacity;
};
void StackInit(struct Stack* ps, int capacity = 4)//缺省参数
{
ps->a = (int*)malloc(sizeof(int) * capacity);
ps->top = 0;
ps->capacity = capacity;
}
void StackPush(struct Stack* ps, int x)
{
//..........
}
int main()
{
struct Stack st;
//StackInit(&st);//不知道栈最多存多少数据,就用缺省值初始化
StackInit(&st, 100);//知道栈最多存100数据,显示传值,这样可以减少增容次数,提高效率
}
函数重载
函数重载概念
函数重载:是函数的一种特殊情况,C++允许在同一作用域中声明几个功能类似的同名函数,这些同名函数的形参列表(参数个数 或 类型 或 顺序)必须不同,常用来处理实现功能类似数据类型不同的问题
1.参数类型不同
int Add(int left, int right)
{
cout << "int Add(int left, int right)" << endl;
return left + right;
}
double Add(double left, double right)
{
cout << "double Add(double left, double right)" << endl;
return left + right;
}
2.参数个数不同
void f()
{
cout << "f()" << endl;
}
void f(int a)
{
cout << "f(int a)" << endl;
}
3.参数顺序不同
void f(int a, char b)
{
cout << "f(int a, char b)" << endl;
}
void f(char b, int a)
{
cout << "f(char b, int a)" << endl;
}
4.返回值不同,缺省参数不同,无法构成函数重载
double f(double d)
{}
void f(double d)
{}
int f(int a = 3)
{}
int f(int a = 4)
{}
5.构成重载,但是使用时会有问题,调用存在歧义
void f()
{
cout << "f()" << endl;
}
void f(int a = 4)
{
cout << "f(int a = 4)" << endl;
}
int main()
{
f();//调用存在歧义
f(1);
return 0;
}
引用
引用概念
引用不是新定义一个变量,而是给已存在变量取了一个别名,编译器不会为引用变量开辟内存空间,它和它引用的变量共用同一块内存空间
#include <iostream>
using namespace std;
int main()
{
int a = 10;
//引用定义
int& b = a;
//取地址
int* p = &b;
a = 30;
b = 40;
return 0;
}
注意:引用类型必须和引用实体是同种类型的
引用特性
引用在定义时必须初始化
一个变量可以有多个引用
引用一旦引用一个实体,再不能引用其他实体
int a = 10;
int& b;
int a = 10;
int& b = a;
int& c = a;
int& d = b;
double d = 1.1;
int a = 10;
int&b = a;
int c = 20;
b = c;//这里把c赋值给b
引用使用场景
引用做参数
void swap(int* p1, int* p2)//传地址
{
int tmp = *p1;
*p1 = *p2;
*p2 = tmp;
}
void swap(int& r1, int& r2)//传引用
{
int tmp = r1;
r1 = r2;
r2 = tmp;
}
void swap(int r1, int r2)//传值
{
int tmp = r1,
r1 = r2;
r2 = tmp;
}
int main()
{
int x = 0, y = 0;
swap(x, y);
swap(&x, &y);
return 0;
}
//它们三个构成函数重载
//但是swap(x, y);调用时存在歧义,不清楚传值还是传引用
//类似之前说的那个,它们构成重载,但是存在歧义
void f();
void f(int x = 0, int y = 0);
在leetcode中,我们常常会遇到这种情况
//int* singleNumbers(int* nums, int numsSize, int* returnSize)
int* singleNumbers(int* nums, int numsSize, int& returnSize)
{
int* a = (int*)malloc (sizeof(int) * returnSize);
//......
//*returnSize = 2;
returnSize = 2;
return a;
}
二级指针,传引用可以代替二级指针
int a = 10;
int* p1 = &a;
int*& p2 = p1;
void SListPushBack(SLTNode*& phead, SLTDateType x);
void SListPushBack(SLTNode*& phead, SLTDateType x)
{
//...........
}
int main()
{
SLTNode* plist = NULL;
SListPushBack(plist, 1);
SListPushBack(plist, 2);
return 0;
}
引用做返回值
//传值返回
int Add(int a, int b)
{
int c = a + b;
return c;
}
int main()
{
int ret = Add(1, 2);
cout << ret << endl;//3
return 0;
}
//传引用返回
int& Add(int a, int b)
{
int c = a+ b;
return c;
}
int main()
{
int& ret = Add(1, 2);//ret就是c的引用(别名)
cout << ret << endl;//3
Add(10, 20);//30
printf("1111111111111111111111\n");
cout << ret << endl;//随机值
return 0;
}
注意:所有的传值返回都会生成一个拷贝 – 临时变量int tmp,也就是将临时变量赋值给ret,那么就有一个问题,临时变量存在哪呢?
1.如果c比较小(4 or 8),一般是寄存器充当临时变量
2.如果c比较大,临时变量放在调用Add函数的栈帧中
传引用返回的意思就是不会生成c的拷贝返回,直接返回c的引用(别名)int& tmp。传引用返回这个代码存在问题:1.存在非法访问,因为Add(1,2)的返回值是c的引用,所有Add栈帧销毁之后,回去访问c的内存空间。2.如果Add函数栈帧销毁,清理空间,那么取c值的时候取到就是随机值,赋值给ret就是随机值,当前这个取决于编译器实现了。vs下销毁栈帧,并没有清除空间数据。
如果函数返回时,出了函数作用域,如果返回对象还在(还未还给操作系统,比如静态变量或者全局变量),则可以使用传引用返回,如果已经还给操作系统了,则必须使用传值返回
传值,传引用效率比较
以值作为参数或者返回值类型,在传参和返回期间,函数不会直接传递实参或者将变量本身直接返回,而是传递实参或者返回变量的一份临时的拷贝,因此用值作为参数或者返回值类型,效率是非常低下的,尤其是当参数或者返回值类型非常大时,效率就更低。
#include <time.h>
struct A
{
int a[10000];
};
void TestFunc1(struct A a)
{}
void TestFunc2(struct A& a)
{}
void TestRefAndValue()
{
struct A a;
// 以值作为函数参数
size_t begin1 = clock();
for (size_t i = 0; i < 10000; ++i)
TestFunc1(a);
size_t end1 = clock();
// 以引用作为函数参数
size_t begin2 = clock();
for (size_t i = 0; i < 10000; ++i)
TestFunc2(a);
size_t end2 = clock();
// 分别计算两个函数运行结束后的时间
cout << "TestFunc1(A)-time:" << end1 - begin1 << endl;
cout << "TestFunc2(A&)-time:" << end2 - begin2 << endl;
}
传值,传引用返回性能比较
#include <time.h>
struct A
{
int a[10000];
};
struct A a;
// 传值返回
struct A TestFunc1()
{
return a;
}
// 传引用返回
struct A& TestFunc2()
{
return a;
}
void TestReturnByRefOrValue()
{
// 以值作为函数的返回值类型
size_t begin1 = clock();
for (size_t i = 0; i < 100000; ++i)
TestFunc1();
size_t end1 = clock();
// 以引用作为函数的返回值类型
size_t begin2 = clock();
for (size_t i = 0; i < 100000; ++i)
TestFunc2();
size_t end2 = clock();
// 计算两个函数运算完成之后的时间
cout << "TestFunc1 time:" << end1 - begin1 << endl;
cout << "TestFunc2 time:" << end2 - begin2 << endl;
}
通过上述代码的比较,传值和传引用或者传值返回和传引用返回效率相差甚大
总结一下:引用的作用主要体现在传参和传返回值
1.传引用和传引用返回,有些场景下面,可以提高性能。(大对象+深拷贝对象)
2.传引用和传引用返回,输出型参数和输出型返回值。通俗点说,有些场景下面,形参的改变可以改变实参。有些场景下面,传引用返回,可以改变返回对象。
常引用
//假设x是一个大对象或者深拷贝对象
//那么尽量使用传引用,减少拷贝。如果函数f不改变x,建议尽量使用const传引用
//void f(int& x)
void f(const int& x)
{
cout << x << endl;
}
int main()
{
//权限放大 不可以
//const int a = 10;
//int& b = a;
//权限不变 可以
const int a = 10;
const int& b = a;
//权限缩小 可以
int c = 10;
const int& d = c;
f(a);
f(c);
f(10);
return 0;
}