目录
命名空间
2.1引入
在C/C++中,变量、函数和后面要学到的类都是大量存在的,这些变量、函数和类的名称将都存在于全局作
用域中,可能会导致很多冲突。使用命名空间的目的是对标识符的名称进行本地化,以避免命名冲突或名字
污染,namespace关键字的出现就是针对这种问题的。
- 下面的代码会报错误,因为 stdlib.h 库里有 rand 函数会造成名字命名冲突
#include<stdio.h>
#include<stdlib.h>
int rand = 0;
int main()
{
printf("%d\n", rand);
return 0;
}
- 解决方式,将 rand 变量放到 sz 这个域中。全局变量在全局域中,直接定义,而下面是自己创建一个 sz 的 域,域会影响访问和生命周期,比如下面的 rand 放在 sz 域中,访问要以特定的形式 (sz::rand)访问,而且rand的生命周期只在 sz 域中。
// 命名空间域
namespace sz
{
int rand = 0;
}
int main()
{
printf("%d\n", sz::rand);
return 0;
}
- 下面两种的区别
#include<stdio.h>
#include<stdlib.h>
namespace sz
{
int rand = 0;
}
int main()
{
printf("%d\n", rand); // 默认访问全局变量,访问的是stdlib.h 中的rand函数,rand是地址,%d 以整数的形式打印出来了
printf("%d\n", sz::rand); // 访问的是 sz域中的变量 rand
return 0;
}
运行结果:
- 域作用限定符(::)
#include<stdio.h>
#include<stdlib.h>
int a = 0;
int main()
{
int a = 1;
printf("%d\n", a); // 局部优先原则,结果a = 1;
// :: 域作用限定符
printf("%d\n",::a);// 指定去 :: 左边的域去找,::左边为空白表示去全局域去找,结果 a=0;
return 0;
}
运行结果:
2.2 命名空间定义
定义命名空间,需要使用到namespace关键字,后面跟命名空间的名字,然后接一对{}即可,{}中即为命名
空间的成员。
注意:一个命名空间就定义了一个新的作用域,命名空间中的所有内容都局限于该命名空间中
//1. 普通的命名空间
namespace N1 // N1为命名空间的名称
{
// 命名空间中的内容,既可以定义变量,也可以定义函数/结构体
int a;
int Add(int left, int right)
{
return left + right;
}
struct TreeNode
{
struct TreeNode* left;
struct TreeNode* right;
int val;
};
}
//2. 可以嵌套
namespace sql
{
int a = 0;
namespace sz
{
int rand = 0;
void func()
{
printf("func()\n");
}
struct TreeNode
{
struct TreeNode* left;
struct TreeNode* right;
int val;
};
}
}
- 同一个工程中允许存在多个相同名称的命名空间,编译器最后会合成同一个命名空间中。
在 text.cpp 中调用
3.3 命名空间使用
命名空间中成员该如何使用呢?比如:
namespace sz
{
int a = 0;
}
int main()
{
printf("%d\n", a); // 错误,不能访问
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 namespce N;
int main()
{
printf("%d\n", N::a);
printf("%d\n", b);
Add(10, 20);
return 0;
}
}
- std
using namespace std; 中 std是C++标准库的命名空间, 标准库的东西都放在std里面
# include<iostream>
// 当我们要经常使用 cout , cin 和 endl时,要带上 std::会显得很麻烦,如何解决呢?
int main()
{
std::cout << "hello word" << std::endl;
std::cout << "hello word" << std::endl;
std::cout << "hello word" << std::endl;
std::cout << "hello word" << std::endl;
std::cout << "hello word" << std::endl;
return 0;
}
# include<iostream>
// using namespace std;
// 1. 在项目中不推荐使用 using namespace std; 这种全部展开的方式
// 2. 日常练习中使用这种方式
// 3. 项目中推荐,指定命名空间 + 展开常用
using std::cout;
using std::cin;
using std::endl;
int main()
{
cout << "hello word" << endl;
return 0;
}
缺省参数
缺省参数概念
缺省参数是声明或定义函数时为函数的参数指定一个默认值。在调用该函数时,如果没有指定实参则采用该
默认值,否则使用指定的实参。
void Func(int a = 0)
{
cout << a << endl;
}
int main()
{
Func(); // 没有传参时,使用参数的默认值
Func(10); // 传参时,使用指定的实参
}
缺省参数分类
- 全缺省
#include<iostream>
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 << endl;
}
int main()
{
TestFunc();
TestFunc(1); // 从左往右给,a=1,b=20,c=30
//TestFunc(,,1); // 不支持
TestFunc(1, 2); // 从左往右给,a=1,b=2,c=30
TestFunc(1, 2, 3); // 从左往右给,a=1,b=2,c=3
return 0;
}
- 半缺省
#include<iostream>
using namespace std;
// 半缺省
void TestFunc(int a, int b = 20, int c = 10) // 必须从右往左连续缺省,不能间隔
{
cout << "a = " << a << endl;
cout << "b = " << b << endl;
cout << "c = " << c << endl << endl;
}
int main()
{
//TestFunc(); // a 没有缺省参数,必须传参
TestFunc(1); // 从左往右给,a=1,b=20,c=10
return 0;
}
- 缺省参数在实际中的应用(栈)
int main()
{
// 知道我一定会插入100个数据,就可以显示传参数100
// 提前开好空间,插入数据避免扩容
struct Stack st1;
StackInit(&st1, 100);
struct Stack st2;
StackInit(&st2);
return 0;
}
注意:
- 半缺省参数必须从右往左依次来给出,不能间隔着给
- 缺省参数不能在函数声明和定义中同时出现(声明中给)
//a.h
void TestFunc(int a = 10);
// a.c
void TestFunc(int a = 20)
{}
// 注意:如果生命与定义位置同时出现,恰巧两个位置提供的值不同,那编译器就无法确定到底该用那个缺省值。
- 缺省值必须是常量或者全局变量
- C语言不支持(编译器不支持)
引用(&)
概念
引用不是新定义一个变量,而是给已存在变量取了一个别名,编译器不会为引用变量开辟内存空间,它和它
引用的变量共用同一块内存空间。
- 类型& 引用变量名(对象名) = 引用实体;
- 注意:引用类型必须和引用实体是同种类型的;
引用特性
- 引用在定义时必须初始化
- 一个变量可以有多个引用
- 引用一旦引用一个实体,再不能引用其他实体
#include<iostream>
using namespace std;
int main()
{
int a = 1;
// int& b; // 1、引用在定义时必须初始化
int& b = a; // 2、一个变量可以有多个引用
int& c = a;
int& d = c;
++a;
int x = 10;
// 3、引用一旦引用一个实体,再不能引用其他实体
b = x; // b是x的别名呢?还是x赋值给b呢?答案是赋值
return 0;
}
使用场景
- 做参数 --> a、输出型参数 b、大对象传参,提高效率
void Swap(int& left, int& right)
{
int temp = left;
left = right;
right = temp;
}
- 做返回值 – a、输出型返回对象,调用者可以修改返回对象 b、减少拷贝,提高效率
注意:如果函数返回时,出了函数作用域,如果返回对象还未还给系统,则可以使用引用返回,如果已
经还给系统了,则必须使用传值返回
传值、传引用效率比较
以值作为参数或者返回值类型,在传参和返回期间,函数不会直接传递实参或者将变量本身直接返回,而是
传递实参或者返回变量的一份临时的拷贝,因此用值作为参数或者返回值类型,效率是非常低下的,尤其是
当参数或者返回值类型非常大时,效率就更低。
#include <time.h>
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(A)-time:" << end1 - begin1 << endl;
cout << "TestFunc2(A&)-time:" << end2 - begin2 << endl;
}
值和引用的作为返回值类型的性能比较
#include <time.h>
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;
}
引用在栈中的应用
常引用
引用和指针的区别
函数重载
概念
函数重载:是函数的一种特殊情况,C++允许在同一作用域中声明几个功能类似的同名函数,这些同名函数
的形参列表(参数个数 或 类型 或 类型顺序)不同,常用来处理实现功能类似数据类型不同的问题。
#include<iostream>
using namespace std;
// 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;
}
int main()
{
Add(10, 20);
Add(10.1, 20.2);
f();
f(10);
f(10, 'a');
f('a', 10);
return 0;
}
C++支持函数重载的原理–名字修饰(name Mangling)
【扩展学习:C/C++函数调用约定和名字修饰规则–有兴趣好奇的同学可以看看,里面有对vs下函
数名修饰规则讲解】链接
extern “C”
【面试题】
- 下面两个函数能形成函数重载吗?有问题吗或者什么情况下会出问题?
void Func(int a = 10)
{
cout << "void Func(int a = 10)" << endl;
}
void Func(int a)
{
cout << "void Func(int a)" << endl;
}
- C语言中为什么不能支持函数重载?
- C++中函数重载底层是怎么处理的?
- C++中能否将一个函数按照C的风格来编译?
内联函数
概念
特性
// F.h
#include <iostream>
using namespace std;
inline void f(int i);
// F.cpp
#include "F.h"
void f(int i)
{
cout << i << endl;
}
// main.cpp
#include "F.h"
int main()
{
f(10);
return 0;
}
// 链接错误:main.obj : error LNK2019: 无法解析的外部符号 "void __cdecl f(int)" (?
f@@YAXH@Z),该符号在函数 _main 中被引用
宏和内联
auto关键字(C++11)
类型别名思考
auto简介
#include<typeinfo>
#include<iostream>
using namespace std;
int TestAuto()
{
return 10;
}
int main()
{
int a = 10;
auto b = a;
auto c = 'a';
auto d = TestAuto();
cout << typeid(b).name() << endl;
cout << typeid(c).name() << endl;
cout << typeid(d).name() << endl;
//auto e; 无法通过编译,使用auto定义变量时必须对其进行初始化
return 0;
}
auto的使用细则
auto不能推导的场景
基于范围的for循环(C++11)
范围for的语法
范围for的使用条件
指针空值nullptr(C++11)
C++98中的指针空值