auto 的使用场景(c++)

1.auto的功能:auto 可以自动推理数据类型。
auto使用语法如下:

#include<iostream>
#include<typeinfo>
using namespace std;

int main()
{
    //auto变量定义时必须初始化
    auto x = 3;
    auto y{23};
    //auto c;

    //定义一个auto的系列的变量必须始终推倒成同一类型
    auto x1{1}, x2{2};
    //auto a{10}, b{2.0};

    //如果初始化表达式是引用或const,则去除引用或const语义
    int& q{x1};
    auto m {q};
    cout<<"type of m: "<< typeid(m).name() <<endl;   //int
    m = 400;
    cout<< "q : " << q << " m : " << m <<endl;
    const int n{100};
    auto t {n};                       
    cout<<"type of t: "<< typeid(t).name() <<endl;   //int
    t = 300;
    cout<<"t : "<< t <<endl;  

    //如果auto关键字加上&,则不去除引用或const语意
    char ch1{'c'}, &ch2{ch1};
    auto& ch3{ch2};
    cout<<"type of ch3: "<< typeid(ch3).name() <<endl;   //char&
    ch3 = 'b';
    cout<<"ch1 : "<<ch1<<"  ch2 : "<<ch2<<"  ch3: "<<ch3<<endl;

    const double h {1.0};
    auto& k {h};
    cout<< "type of k: "<<typeid(k).name() <<endl; //const double
    //k = 12.0;

    //如果初始化为数组,auto自动推倒为指针
    int arr[3]{1,2,3};
    auto p {arr};
    cout<<"type of p: "<<typeid(p).name()<<endl; //int*
    cout<<"size of arr: "<<sizeof(arr)<<endl;
    cout<<"size of p: "<<sizeof(p)<<endl;

    //如果加上&,则推导为数组
    auto& px{arr};
    cout<<"type of px: "<<typeid(px).name()<<endl; //A3_i
    cout<<"size of px: "<<sizeof(px)<<endl;

    //C++14可以用auto来推倒函数返回值

    return 0;
}

注:auto不能用来定义数组或函数形参。
2.使用场景:
第一种经典场景:当变量的数据类型很复杂或者写起来很麻烦,我们懒得打字了,其实是为了加快编程效率。

#include<iostream>
#include<unordered_map>
#include<string>
using namespace std;

int main()
{
    nordered_multimap<string,double> mymap;
    
    mymap.insert(make_pair("masheng",480)); 
    mymap.insert(make_pair("hafou",500));
    mymap.insert(make_pair("jianqiao",490));
    mymap.insert(make_pair("hafou",499));
    mymap.insert(make_pair("jianqiao",479));

    //查找容器里键值与被查找数据匹配的所有元素,容器里键值相同的元素有多个时,equal_range 就可以上场了
    //数据类型如此之长,为啥不用auto 
    pair<unordered_multimap<string,double>::iterator, unordered_multimap<string,double>::iterator> ifind = mymap.equal_range("jianqiao");
    if (ifind.first != mymap.end()) //查找成功
    {
        for(auto it=ifind.first; it!=ifind.second; it++){
            cout<< it->first << "->" << it->second <<endl;
        }
    } 

    return 0;
}

注:这里的变量 ifind 的数据类型这么长,直接用auto很方便,贼舒服.

int main()
{
    nordered_multimap<string,double> mymap;
    
    mymap.insert(make_pair("masheng",480)); 
    mymap.insert(make_pair("hafou",500));
    mymap.insert(make_pair("jianqiao",490));
    mymap.insert(make_pair("hafou",499));
    mymap.insert(make_pair("jianqiao",479));
    
   	auto ifind = mymap.equal_range("jianqiao");
    if (ifind.first != mymap.end()) 
    {
        for(auto it=ifind.first; it!=ifind.second; it++){
            cout<< it->first << "->" << it->second <<endl;
        }
    } 
 }
	 /*
    jianqiao->479
    jianqiao->490
    */

第二种经典场景: 当我们有时不确定函数返回值类型时, 或者说用auto来推理函数返回值类型。

template<typename T>
auto func(T a, T b){
   
   return max(a,b);
}
int main()
{
   cout << func(100,200) <<endl;
   cout << func(300.88, 400.55) <<endl;
   cout << func('a', 'A') << endl;

   return 0;
}
/*
200
400.55
a
*/

总之,auto虽然方便,但是每一种语言都有自己的类型系统,所以我们不要滥用auto,要在合适的时机使用。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值