以inline为首的函数
inline int add(int a, int b)
{
return a + b;
}
void test()
{
int a = add(10, 20);
}
1.inline是一种以空间换时间的做法,省去调用函数的额外开销。所以代码很长或者有循环/递归的函数不适宜使用作为内联函数
2.inline 对于编译器而言只是一个建议,编译器会自动优化,如果定义为inline的函数体内有循环/递归等等,编译器优化师会忽略掉内敛。
3.inline 不建议声明和定义分离,分离会导致链接错误。因为inline被展开,就没有函数地址了,链接就会找不到。
宏的优缺点
优点:
1.增强代码的复用性
2.提高性能
缺点:
1.不方便调试宏。(因为预编译阶段进行了替换)
2.导致代码可读性差,可维护性差,容易误用。
3.没有类型安全的检查
auto
//定义指针:可加可不加
auto pa=&a;
autopb=&b;
//定义引用:必须加&
auto& rb=b;
auto pb=b;
一行定义多个变量时,表达式推导出的类型必须一致
auto不能使用的场景
1.auto不能作为函数的参数
2.auto不能直接用来声明数组
void test()
{
int arr[] = { 1,2,3,4,5,6,7,8,9 };
for (int i = 0; i < sizeof(arr) / sizeof(arr[0]); ++i)
{
cout << arr[i] << "";
}
cout<<endl;
}
范围for 当前数据:循环的范围
for (const int& e : arr)
{
cout << e << "";
}
cout << endl;
for (const auto& e : arr)
{
cout << e << "";
}
cout << endl;
数组作为参数时,数组名退化成指针,范围不确定,不能使用范围for
void fun(int arr[],int n)
{
for(const<<e<<"";
{
cout<<e<<"";
}
cout<<endl;
}