• 【C++】基础知识点回顾 下:auto关键字、范围内的for循环


    前言

    前两篇文章,我们分别复习了这些知识点
    命名空间与输入输出

    函数重载、引用和内联函数

    今天,我们来学习auto关键字、范围内的for循环

    auto关键字

    定义

    C++11中,标准委员会赋予了auto全新的含义即:auto不再是一个存储类型指示符,而是作为一个新的类型指示符来指示编译器,auto声明的变量必须由编译器在编译时期推导而得

    补充知识点:typeid

    在C++中,typeid是一个运算符,用于获取表达式的类型信息。当你调用typeid(d).name() 时,它将返回一个表示表达式的类型的字符串。这个字符串通常是编译器特定的,可能不易于阅读,但可以用于调试或其他目的。

    要使用typeid,你需要包含头文件

    例子:

    #include
    #include
    
    using namespace std;
    
    int main()
    {
    	int a = 0;
    	char c = 'a';
    
    	cout << typeid(a).name() << endl;
    	cout << typeid(c).name() << endl;
    
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    运行结果:

    在这里插入图片描述
    学习过运算符typeid之后,我们来继续学习auto
    上面提到了,auto会自动推导变量的类型,下面给出一段代码,方便大家理解

    #include
    #include
    
    using namespace std;
    
    int XX()
    {
    	return 1;
    }
    
    int main()
    {
    	auto i = 1;
    	auto c = 'c';
    	int a = 10;
    	auto b = a;
    	auto x = XX();
    
    	cout << typeid(i).name() << endl;
    	cout << typeid(c).name() << endl;
    	cout << typeid(b).name() << endl;
    	cout << typeid(x).name() << endl;
    
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    运行结果:
    在这里插入图片描述
    通过上面这段代码,我们可以知道,auto可以自动推导元素的类型

    详细规则

    变量初始化

    1.使用auto时,必须对其修饰的变量进行初始化,不然编译器无法得知该变量是什么类型,会报错

    auto与指针和引用

    2.auto和指针以及引用结合使用
    需要注意的是:
    auto和指针结合使用的时候,*可写可不写

    auto和引用结合使用的时候,&必须写

    下面给出一段示例代码

    #include
    #include
    
    using namespace std;
    
    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;
    	cout << x << endl;
    
    	*b = 30;
    	cout << x << endl;
    
    	c = 40;
    	cout << x << endl;
    
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    运行结果:
    在这里插入图片描述

    一次定义多个变量

    在同一行定义多个变量的时候,要注意:这些变量的类型都必须相同
    不然程序会报错

    下面给出一个例子:

    #include
    
    using namespace std;
    
    int main()
    {
    	auto a = 1, b = 2, c = 3;
    
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    auto不能做的

    auto定义的变量不能作为函数的参数

    auto不能直接定义数组

    范围内的for循环

    定义

    在C++11中,引入了一种新的for循环,这种循环是在一定的范围内直接进行循环

    语法格式

    for (element_type variable : container)
    {
    
    }
    
    • 1
    • 2
    • 3
    • 4

    element_type是要便利的容器内元素的类型
    variable 是变量名称,类似于给容器内单个元素进行命名
    container是要遍历的容器的名称

    给出一个例子:

    #include
    
    using namespace std;
    
    int main()
    {
    	int arr[] = { 1,2,3,4,5 };
    
    	for (int num : arr)
    	{
    		cout << num << "  ";
    	}
    	cout << endl << "-------------" << endl;
    
    	for (int xx : arr)
    	{
    		cout << xx << "  ";
    	}
    
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    运行结果:
    在这里插入图片描述

    nullptr

    在C++11中nullptr作为关键字引入,表示空指针,

    在需要用到空指针的地方,建议用nullptr而不是null

    在此不多做解释

    结语

    关于auto关键字、范围内的for循环的学习和介绍到这里就结束了,希望对大家有帮助,我们下次见~

  • 相关阅读:
    Allure的下载和部署
    小项目----音乐在线播放器
    【C++】仿函数
    【C++】智能指针
    Java反射详解,还有什么理由学不会
    mysql数据库的安装教程
    解决创建conda环境时Solving environment: failed 和 ResolvePackageNotFound 的错误
    leetcode栈和队列三剑客
    linux 系统时间、时区、date、timedatectl
    信贷风控NCL净损失率的指标实现与应用
  • 原文地址:https://blog.csdn.net/cat_with_cat/article/details/132872426