1.C++关键字
2.命名空间
2.1 命名空间的定义
1. 普通的命名空间
namespace N1 //N1为命名空间的名称
{
// 命名空间中的内容,既可以定义变量,也可以定义函数
int a;
int Add(int left, int right)
{
return left + right;
}
}
2.嵌套命名
namespace N2
{
int a;
int Add(int left, int right)
{
return left + right;
}
namespace N3
{
int b = 5;
int sub(int left, int right)
{
return left - right;
}
}
}
3. 同一个工程中允许存在多个相同名称的命名空间 , 编译器后会合成同一个命名空间中
namespace N1//在同一个工程中,此处的N1会与上面普通命名空间N1合在一起
{
int Mul(int left, int right)
{
return left * right;
}
}
2.2 命名空间的使用
命名空间的使用有三种方式:
①:加命名空间名称及作用域限定符
int main()
{
printf("%d\n", N1::a);
return 0;
}
②:使用using将命名空间中成员引入
using N1::a;
int main()
{
printf("%d\n", a);
return 0;
}
③:使用using namespace 命名空间名称引入
using namespce N1;
int main()
{
printf("%d\n", b);
Add(10, 20);
return 0;
}
3. C++输入&输出
#include
using namespace std;
int main()
{
cout << "Hello world!!!" << endl;
return 0;
}
4.缺省参数
- 4.1缺省参数概念
缺省参数是声明或定义函数时为函数的参数指定一个默认值。在调用该函数时,如果没有指定实参则采用该默 认值,否则使用指定的实参。
#include
using namespace std;
void TestFunc(int a = 0)
{
cout << a << endl;
}
int main()
{
TestFunc();// 没有传参时,使用参数的默认值
TestFunc(10);// 传参时,使用指定的实参
}
4.2 缺省参数分类
①:全缺省参数
#include
using namespace std;
void TestFunc(int a = 10, int b = 20,int c = 30)
{
cout << "a = " << a << endl;
cout << "b = " << b << endl;
cout << "c = " << c << endl;
}
int main()
{
TestFunc();
system("pause");
return 0;
}
②:半缺省参数
#include
using namespace std;
// 半缺省参数必须从右往左依次来给出,不能间隔着给.
void TestFunc(int a, int b ,int c = 30)
{
cout << "a = " << a << endl;
cout << "b = " << b << endl;
cout << "c = " << c << endl;
}
int main()
{
TestFunc(5,6);
system("pause");
return 0;
}
缺省参数应该注意的几点:
a:半缺省参数必须从右往左依次来给出,不能间隔着给.
b:缺省参数不能在函数声明和定义中同时出现.
//a.h
void TestFunc(int a = 10);
//a.c
void TestFunc(int a = 20)
{
}
注意:如果生命与定义位置同时出现,恰巧两个位置提供的值不同,
那么编译器就无法确定到底该用那个缺省值。
c.缺省值必须是常量或者全局变量.
#include
using namespace std;
int x = 100;
int h = 200;
void TestFunc(int a = h, int b = x, int c = 30)//3. 缺省值必须是常量或者全局变量
{
cout << "a = " << a << endl;
cout << "b = " << b << endl;
cout << "c = " << c << endl;
}
int main()
{
TestFunc();
system("pause");
return 0;
}
d. C语言不支持( 编译器不支持 ).
5.函数重载
- 5.1函数重载概念
函数重载是函数的一种特殊情况,C++允许在同一作用域中声明几个功能类似 的同名函数,这些同名函数的形参列表(参数个数 或 类型 或 顺序)必须不同, 常用来处理实现功能类似数据类型不同的问题
举个例子:
int Add(int left, int right)
{
return left + right;
}
double Add(double left, double right)
{
return left + right;
}
long Add(long left, long right)
{
return left + right;
}
int main()
{
Add(10, 20); Add(10.0, 20.0); Add(10L, 20L);
return;
}
- 5.2 名字修饰(name Mangling)
①:在C/C++中,一个程序要运行起来,需要经历以下几个阶段:预处理、编译、汇编、链接。
之前的博客有提到过,在这里附上链接:https://blog.csdn.net/Adenson/article/details/93665508
②:Name Mangling是一种在编译过程中,将函数、变量的名称重新改编的机 制,简单来说就是编译器为了区分各个函数,将函数通过某种算法,重新修饰为一个全局唯一的名称。
③:C语言的名字修饰规则非常简单,只是在函数名字前面添加了下划线。比如在这里举个例子,对于以下代码:
int Add(int left, int right);
int main()
{
printf("%d\n", Add(5, 6));
system("pause");
return 0;
}
在vs下,对上述代码进行编译链接,最后编译器报错: error LNK2019: 无法解析的外部符号 _Add,该符号在函数 _main 中被引用:
注意:上述例子中,Add函数只给了声明没有给定义,因此在链接时就会报错,提示:在main函数中引用的Add函数找不到函数体。从报错结果中可以看到,C语言只是简单的在函数名前添加下划线。因此当工程中存在相同函数名的函数时,就会产生冲突。所以可以因此得出结论: C语言中为不能支持函数重载。
④:由于C++要支持函数重载,命名空间等,使得其修饰规则比较复杂,不同编译器在底层的实现方式可能都有差异,比如在这里举个例子,对于以下代码:
int Add(int left, int right);
int Add(double left, double right);
int main()
{
cout << Add(5, 6) << endl;
cout << Add(5.2, 6.3) << endl;
system("pause");
return 0;
}
在vs下,对上述代码进行编译链接,最后编译器报错:
error LNK2019: 无法解析的外部符号 “double cdecl Add(double,double)” (?Add@@YANNN@Z)
error LNK2019: 无法解析的外部符号 “int __cdecl Add(int,int)” (?Add@@YAHHH@Z)
通过上述错误可以看出,编译器实际在底层使用的不是Add名字,而是被重新修饰过的一个比较复杂的名字,被重新修饰后的名字中包含了:函数的名字以及参数类型。这就是为什么函数重载中几个同名函数要求其参数列表不同的原因。只要参数列表不同,编译器在编译时通过对函数名字进行重新修饰,将参数类型包含在最终的名字中,就可保证名字在底层的全局唯一性。所以可以因此得出结论: C++语言可以支持函数重载。
附上C++中基本函数在函数底层此修饰后的民名称:
- 5.3 extern “C”
有时候在C++工程中可能需要将某些函数按照C的风格来编译,在函数前加extern “C”,意思是告诉编译器,将该函数按照C语言规则来编译。
extern "C"int Add(double left, double right);
int main()
{
cout << Add(5, 6) << endl;
system("pause");
return 0;
}
链接时报错:error LNK2019: 无法解析的外部符号_Add,该符号在函数 _main 中被引用.
6. 引用
- 6.1引用的概念
引用不是新定义一个变量,而是给已存在变量取了一个别名,编译器不会为引用变量开辟内存空间,它和它引用的变量共用同一块内存空间。引用方式为:类型& 引用变量名(对象名) = 引用实体;
int main()
{
int a = 10;
int& b = a;
cout << a << endl << b << endl;
cout << &a << endl << &b << endl;
system("pause");
return 0;
}
- 6.2引用的特性
a. 引用在定义时必须初始化
b. 一个变量可以有多个引用
c. 引用一旦引用一个实体,再不能引用其他实体
int main()
{
int a = 10;
//int &ra;//这句代码是错误的,编译时会出错
int& pa = a;
int& ppa = a;
system("pause");
return 0;
}
- 6.3 常引用
void TestConstRef()
{
const int a = 10;
//int& ra = a; // 该语句编译时会出错,a为常量
const int& ra = a;//因为 a 是只读的,所以 ra 也只能变为只读
// int& b = 10; // 该语句编译时会出错,b为常量
const int& b = 10;
double d = 12.34;
//int& rd = d; // 该语句编译时会出错,类型不同
const int& rd = d;//常引用就会避免这种情况
}
在这里我们应该注意一点,对于指针和引用赋值:
①:权限的缩小是 OK 的(例如:从可读可写 ------>到只可读或者只可写)
②:权限的放大是 NO 的(例如:只可读或者只可写------>到可读可写)
6.4 引用使用场景
void Swap(int& left, int& right)
//因为left 和 right 分别是 a 和 b 的别名,
//改变left 和 right 相当于该变 a 和 b
{
int tmp = left;
left = right;
right = tmp;
}
int main()
{
int a = 20;
int b = 10;
Swap(a, b);
cout << a << endl << b << endl;
//输出为 10 ,20,可以看出已经交换 a 和 b 的值
system("pause");
return 0;
}
int& TestRefReturn(int& pa)
{
pa += 3;
return pa;
}
int main( )
{
int a = 10;
int& ret = TestRefReturn(a);
cout << ret << endl;//输出为13
system("pause");
return 0;
}
看一个比较奇怪的代码:
int& Add(int a, int b)
{
int c = a + b;
return c;
}
int main()
{
int& ret = Add(1, 2);
Add(3, 4);
cout << "Add(1, 2) is :" << ret << endl;
//结果输出为 7 而不为 3,想想为什么?
system("pause");
return 0;
}
原因是:如果函数返回时,离开函数作用域后,其栈上空间已经还给系统,因此不能用栈上的空间作为引用类型返回。如果以引用类型返回,返回值的生命周期必须不受函数的限制(即比函数生命周期长)
- 6.5 传值、传引用效率比较
以值作为参数或者返回值类型,在传参和返回期间,函数不会直接传递实参或者将变量本身直接返回,而是传递实参或者返回变量的一份临时的拷贝,因此用值作为参数或者返回值类型,效率是非常低下的,尤其是当参数或者返回值类型非常大时,效率就更低。
①:值和引用传参的性能比较:
#include
struct A
{
int a[10000];
};
void TestFunc1(A a)
{}
void TestFunc2(A& a)
{}
void TestRefAndValue()
{
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(int*)-time:" << end1 - begin1 << endl;
cout << "TestFunc2(int&)-time:" << end2 - begin2 << endl;
}
// 运行多次,检测值和引用在传参方面的效率区别
int main()
{
for (int i = 0; i < 10; ++i)
{
TestRefAndValue();
}
system("pause");
return 0;
}
运行十次比较效率,输出:
//TestFunc1(int*) - time:47
//TestFunc2(int&) - time : 0
//TestFunc1(int*) - time : 47
//TestFunc2(int&) - time : 0
//TestFunc1(int*) - time : 46
//TestFunc2(int&) - time : 1
//TestFunc1(int*) - time : 35
//TestFunc2(int&) - time : 0
//TestFunc1(int*) - time : 35
//TestFunc2(int&) - time : 1
//TestFunc1(int*) - time : 35
//TestFunc2(int&) - time : 1
//TestFunc1(int*) - time : 31
//TestFunc2(int&) - time : 0
//TestFunc1(int*) - time : 31
//TestFunc2(int&) - time : 0
//TestFunc1(int*) - time : 33
//TestFunc2(int&) - time : 0
//TestFunc1(int*) - time : 31
//TestFunc2(int&) - time : 0
②:值和引用的作为返回值类型的性能比较:
#include
struct A
{
int a[10000];
};
A a;
A TestFunc1()
{
return a;
}
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;
}
// 测试运行10次,值和引用作为返回值效率方面的区别
int main()
{
for (int i = 0; i < 10; ++i)
TestReturnByRefOrValue();
system("pause");
return 0;
}
运行十次比较效率,输出:
//TestFunc1 time : 647
//TestFunc2 time : 0
//TestFunc1 time : 640
//TestFunc2 time : 3
//TestFunc1 time : 601
//TestFunc2 time : 3
//TestFunc1 time : 595
//TestFunc2 time : 0
//TestFunc1 time : 598
//TestFunc2 time : 0
//TestFunc1 time : 578
//TestFunc2 time : 16
//TestFunc1 time : 711
//TestFunc2 time : 3
//TestFunc1 time : 668
//TestFunc2 time : 3
//TestFunc1 time : 670
//TestFunc2 time : 3
//TestFunc1 time : 670
//TestFunc2 time : 9
通过上述代码的比较,发现传值和指针在作为传参以及返回值类型上效率相差很大。
6.6 引用和指针的区别
①.相同点:
在底层实现上实际是有空间的,实现方式与指针相同,因为引用是按照指针方式来实现的。
int main()
{
int a = 10;
int& ra = a;
ra = 20;
int* pa = &a;
*pa = 20;
system("pause");
return 0;
}
a. 引用在定义时必须初始化,指针没有要求
b. 引用在初始化时引用一个实体后,就不能再引用其他实体,而指针可以在任何时候指向任何一个同类型实体
c. 没有NULL引用,但有NULL指针
d. 在sizeof中含义不同:引用结果为引用类型的大小,但指针始终是地址空间所占字节个数(32位平台下占4个字节)
e. 引用自加即引用的实体增加1,指针自加即指针向后偏移一个类型的大小
f. 有多级指针,但是没有多级引用
g. 访问实体方式不同,指针需要显式解引用,引用编译器自己处理
h. 引用比指针使用起来相对更安全
7.内联函数
- 7.1 概念
以 inline 修饰的函数叫做内联函数,编译时C++编译器会在调用内联函数的地方展开,没有函数压栈的开销,内联函数提升程序运行的效率。
未用 inline 修饰的函数在Debug下转到反汇编是这样子的:
如果在上述函数前增加 inline 关键字将其改成内联函数,在编译期间编译器会用函数体替换函数的调用。
加了 inline 修饰的函数载Debug下转到反汇编是这样子的: 
- 7.2 特性
a. inline是一种 以空间换时间 的做法,省去调用函数额开销,内联函数在编译时直接将函数体插入函数调用的地方,所以代码很长或者有循环/递归的函数不适宜使 用作为内联函数。
b. inline对于编译器而言只是一个建议,编译器会自动优化,如果定义为inline的函数体内有循环/递归等 等,编译器优化时会忽略掉内联。
c. inline不建议声明和定义分离,分离会导致链接错误。因为inline被展开,就没有函数地址了,链接就会找 不到.
d.内联函数在最终生成的代码中是没有定义的;C++编译器直接将函数体插入函数调用的地方;内联函数没有普通函数调用时的额外开销(压栈,跳转,返回)。
- 7.3有关宏的一些问题
宏的 优点:
宏的 缺点:
1.不方便调试。(因为预编译阶段进行了替换)
2.导致代码可读性差,可维护性差,容易误用。
3.没有类型安全的检查
C++有哪些技术替代宏?
8. auto关键字(C++11)
- 8.1 auto简介
int TestAuto()
{
return 10;
}
int main()
{
int a = 10;
auto b = a;
auto c = &a;
auto d = 'a';
auto e = TestAuto();
cout << typeid(b).name() << endl;//输出为 int
cout << typeid(c).name() << endl;//输出为 int*
cout << typeid(d).name() << endl;//输出为 char
cout << typeid(e).name() << endl;//输出为 int
//auto e; 无法通过编译,使用auto定义变量时必须对其进行初始化
system("pause");
return 0;
}
- 8.2 auto使用细则
int main()
{
int x = 10;
auto a = &x;
auto* b = &x;
//a 和 b 都是 x 地址
//用auto声明指针类型时,用 auto 和 auto* 没有任何区别
auto& c = x;
//但用auto声明引用类型时则必须加 &
cout << typeid(a).name() << endl;//输出为 int*
cout << typeid(b).name() << endl;//输出为 int*
cout << typeid(c).name() << endl;//输出为 int
*a = 20;
cout << x << endl;//输出为 20
*b = 30;
cout << x << endl;//输出为 30
c = 40;
cout << x << endl;//输出为 40
system("pause");
return 0;
}
void TestAuto()
{
auto a = 1, b = 2;
auto c = 3, d = 4.0;
//该行代码会编译失败,因为c和d的初始化表达式类型不同
}
- 8.2 auto不能推导的场景
// 此处代码编译失败,auto不能作为形参类型,因为编译器无法对a的实际类型进行推导
//这段代码是错误的。
void TestAuto(auto a)
{
}
void TestAuto()
{
//这段代码是错误的,auto 不能直接用来声明数组
auto b[] = { 4,5,6 };
}
9. 基于范围的for循环(C++11)
void TestFor()
{
int array[] = { 1, 2, 3, 4, 5 };
for (auto e : array)
cout << e << " ";
}
代码解释:在上面的for循环中,e是array中的一个元素,auto则是让编译器自动推导出 e 的类型(在上面的使用情况中 e 被自动推导为int)。在 e 的定义之后,紧跟着一个冒号“:”,之后直接写上需要遍历的表达式,for循环将自动以表达式返回的容器为范围进行迭代。
void TestFor()
{
int array[] = { 1, 2, 3, 4, 5 };
for (int e : array)
cout << e << " ";
}
void TestFor()
{
int array[] = { 1, 2, 3, 4, 5 };
for (auto& e : array)
e *= 2;
//对数组中每一个元素都乘 2 倍操作
//也可以进行访问,比如下面:
//for (auto& e : array)
//cout << e << " ";
for (auto e : array)
cout << e << " ";
//输出结果为:2, 4,6, 8,10
}
void TestFor()
{
int array[] = { 1, 2, 3, 4, 5 };
for (auto& e : array)
{
e *= 2;
break;
}//第一个元素乘 2 后,结束循环
for (int e : array)
cout << e << " ";//输出 2,2,3,4,5,
}
9.2 范围for的使用条件
① :for循环迭代的范围必须是确定的:如数组的第一个元素和最后一个元素便构成了for选好的迭代范围。
②:对于用户自定义的类:若类中定义便实现的有begin、end函数,则这个begin、end便是for循环的迭代范围。
③:基于范围的for循环要求迭代器的对象实现:++ ==等操作符。
④:对于STL标准模板库中(如:vector , set , list ,map , queue, deque, string等)的各种容器使用“基于范围的for循环”是不会有任何问题的,因为这些容器中都定义了相关操作。
10. 指针空值nullptr(C++11)
10.1 C++98中的指针空值
在我么进行编程时,声明一个变量时最好好给该变量一个合适的初始值,否则可能会出现不可预料的错 误,比如未初始化的指针。如果一个指针没有合法的指向,我们基本都是按照如下方式对其进行初始化:
void testptr()
{
int* p1 = NULL;
int* p2 = 0;
}
在传统的C头文件(stddef.h)中,NULL实际是一个宏:
可以看到,NULL可能被定义为字面常量 0,或者被定义为无类型指针(void*)的常量。不论采取何种定义,在 使用空值的指针时,都不可避免的会遇到一些麻烦,比如:
void f1(int*)
{
cout << "f(int*)" << endl;
}
void f1(int x)
{
cout << "f(int)" << endl;
}
int main()
{
f1(NULL);//输出 f(int)
f1((int*)NULL);//输出 f(int*)
f1(0);//输出 f(int)
system("pause");
return 0;
}
我的本意是想通过 f(NULL) 调用指针版本的 f( int* ) 函数,但是由于NULL被定义成0,因此与程序的初衷相悖,调用了f (int )。 在C++98中,字面常量0既可以是一个整形数字,也可以是无类型的指针(void*)常量,但是编译器默认情况下 将其看成是一个整形常量,如果要将其按照指针方式来使用,必须对其进行强转(void *)0 。
10.2 nullptr 与 nullptr_t
①:为了考虑兼容性,C++11并没有消除常量0的二义性,C++11给出了全新的nullptr表示空值指针,。因此:为了避免混淆,C++11提供了nullptr, 即:nullptr代表一个指针空值常量。nullptr是有类型的,其类型为nullptr_t .
②:注意几点:
a. 在使用nullptr表示指针空值时,不需要包含头文件,因为nullptr是C++11作为新关键字引入的。
b. 在C++11中,sizeof(nullptr) 与 sizeof((void*)0)所占的字节数相同。
c. 为了提高代码的健壮性,在后续表示指针空值时建议使用nullptr。