1. auto关键字:
1.1 auto的用法以及和typedef的区别:
“auto”是C++11中引入的一个关键字,用于自动类型推导。它可以让编译器根据初始化表达式自动推断变量的类型,从而简化代码并提高可读性。
那么我们为什么要用到自动识别类型呢?这是因为:
随着程序越来越复杂,程序中用到的类型也越来越复杂,经常体现在:
1. 类型难于拼写
2. 含义不明确导致容易出错
具体有哪些场景呢?请看下述代码:
#include <string>
#include <map>
int main()
{
std::map<std::string, std::string> m{ { "apple", "苹果" }, { "orange",
"橙子" }, {"pear","梨"} };
std::map<std::string, std::string>::iterator it = m.begin();
while (it != m.end())
{
//....
}
return 0;
}
#include <string>
#include <map>
typedef std::map<std::string, std::string> Map;
int main()
{
Map m{ { "apple", "苹果" },{ "orange", "橙子" }, {"pear","梨"} };
Map::iterator it = m.begin();
while (it != m.end())
{
//....
}
return 0;
}
typedef char* pstring;
int main()
{
const pstring p1; // 编译成功还是失败?
const pstring* p2; // 编译成功还是失败?
return 0;
}
1.2 auto的概述:
原来在早期C/C++中auto的含义是:使用auto修饰的变量,是具有自动存储器的局部变量,但遗憾的 是一直没有人去使用它,大家可思考下为什么? C++11中,标准委员会赋予了auto全新的含义即:auto不再是一个存储类型指示符,而是作为一 个新的类型指示符来指示编译器,auto声明的变量必须由编译器在编译时期推导而得。
请看下述代码:
int TestAuto()
{
return 10;
}
int main()
{
int a = 10;
auto b = a;
auto c = 'a';
auto d = TestAuto();
cout << typeid(b).name() << endl;// int
cout << typeid(c).name() << endl;// char
cout << typeid(d).name() << endl;// int
//auto e; 无法通过编译,使用auto定义变量时必须对其进行初始化
return 0;
}
1.3 auto的使用细则:
1.3.1 auto与指针和引用结合起来使用:
上代码!:
int main()
{
int x = 10;
auto a = &x;
auto* b = &x;
auto& c = x;
cout << typeid(a).name() << endl;
cout << typeid(b).name() << endl;
cout << typeid(c).name() << endl;
*a = 20;
*b = 30;
c = 40;
return 0;
}
结论:用auto声明指针类型时,用auto和auto*没有任何区别,但用auto声明引用类型时则必须 加&,但是=x的时候不能用auto*哦~
1.3.2 在同一行定义多个变量 :
void TestAuto()
{
auto a = 1, b = 2;
auto c = 3, d = 4.0; // 该行代码会编译失败,因为c和d的初始化表达式类型不同
}
1.3.3 auto不能推导的场景:
1.3.3.1 auto不能作为函数的参数:
void TestAuto(auto a)
{
//...
}
上述代码会编译失败,auto不能作为形参类型,因为编译器无法对a的实际类型进行推导.
1.3.3.2 auto不能直接用来声明数组:
void TestAuto()
{
int a[] = {1,2,3};
auto b[] = {4,5,6};
}
3. 为了避免与C++98中的auto发生混淆,C++11只保留了auto作为类型指示符的用法
2 范围for循环:
2.1 范围for的语法 :
void TestFor()
{
int array[] = { 1, 2, 3, 4, 5 };
for (int i = 0; i < sizeof(array) / sizeof(array[0]); ++i)
array[i] *= 2;
for (int* p = array; p < array + sizeof(array)/ sizeof(array[0]); ++p)
cout << *p << endl;
}
void TestFor()
{
int array[] = { 1, 2, 3, 4, 5 };
for(auto& e : array)
e *= 2;
for(auto e : array)
cout << e << " ";
return 0;
}
上述代码中:就是范围for循环遍历数组的新方式,范围for也有缺点:虽然看着简洁一点,但是有些功能是达不到的,比如:逆序遍历,从数组中间位置开始遍历等等都做不到...
2.2 范围for的使用条件:
2.2.1 for循环迭代的范围必须是确定的
void TestFor(int array[])
{
for(auto& e : array)
cout<< e <<endl;
}
2.2.2 迭代的对象要实现++和==的操作
关于迭代器这个问题,以后会讲,现在提一下,没办法 讲清楚,现在小伙伴们先了解一下就可以了
3.1 指针空值:
C++98中的指针空值:nullptr
在良好的C/C++编程习惯中,声明一个变量时最好给该变量一个合适的初始值,否则可能会出现 不可预料的错误,比如未初始化的指针。如果一个指针没有合法的指向,我们基本都是按照如下方式对其进行初始化:
void TestPtr()
{
int* p1 = NULL;
int* p2 = 0;
// ……
}
#ifndef NULL
#ifdef __cplusplus
#define NULL 0
#else
#define NULL ((void *)0)
#endif
#endif
可以看到,NULL可能被定义为字面常量0,或者被定义为无类型指针(void*)的常量。不论采取何种定义,在使用空值的指针时,都不可避免的会遇到一些麻烦,比如:
void f(int)
{
cout<<"f(int)"<<endl;
}
void f(int*)
{
cout<<"f(int*)"<<endl;
}
int main()
{
f(0);
f(NULL);
f((int*)NULL);
return 0;
}