[{"createTime":1735734952000,"id":1,"img":"hwy_ms_500_252.jpeg","link":"https://activity.huaweicloud.com/cps.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=V1g3MDY4NTY=&utm_medium=cps&utm_campaign=201905","name":"华为云秒杀","status":9,"txt":"华为云38元秒杀","type":1,"updateTime":1735747411000,"userId":3},{"createTime":1736173885000,"id":2,"img":"txy_480_300.png","link":"https://cloud.tencent.com/act/cps/redirect?redirect=1077&cps_key=edb15096bfff75effaaa8c8bb66138bd&from=console","name":"腾讯云秒杀","status":9,"txt":"腾讯云限量秒杀","type":1,"updateTime":1736173885000,"userId":3},{"createTime":1736177492000,"id":3,"img":"aly_251_140.png","link":"https://www.aliyun.com/minisite/goods?userCode=pwp8kmv3","memo":"","name":"阿里云","status":9,"txt":"阿里云2折起","type":1,"updateTime":1736177492000,"userId":3},{"createTime":1735660800000,"id":4,"img":"vultr_560_300.png","link":"https://www.vultr.com/?ref=9603742-8H","name":"Vultr","status":9,"txt":"Vultr送$100","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":5,"img":"jdy_663_320.jpg","link":"https://3.cn/2ay1-e5t","name":"京东云","status":9,"txt":"京东云特惠专区","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":6,"img":"new_ads.png","link":"https://www.iodraw.com/ads","name":"发布广告","status":9,"txt":"发布广告","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":7,"img":"yun_910_50.png","link":"https://activity.huaweicloud.com/discount_area_v5/index.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=aXhpYW95YW5nOA===&utm_medium=cps&utm_campaign=201905","name":"底部","status":9,"txt":"高性能云服务器2折起","type":2,"updateTime":1735660800000,"userId":3}]
c++11中auto并不代表一种实际的数据类型,它只是一个类型声明的占位符,auto也并不是再所有场景下都能推导出变量的实际类型,使用auto不需要进行初始化,让编译器推导出它的实际类型,再编译阶段将auto占位符替换为真正的类型。
auto temp = 2;
auto还可以和指针,引用以及const,在不同的场景下有对应的推导规则.
当变量不是指针或者引用时,推导的结果中不会保留const关键字
当变量是指针或者引用时,推导结果中会保留const关键字
int temp = 110; auto *a = &temp; //int auto b = &temp; //int * auto &c = temp;
//int auto d = temp; //int int tmp = 250; const auto a1 = tmp; //int auto a2 =
a1; //int 因为没有声明是指针或者引用所以是int const auto &a3 = tmp; //int auto &a4 = a3;
//const int
auto的限制
auto 不能作为函数参数,因为函数参数只有在函数调用的时候才会将实参传入,auto要求必须给修饰的变量赋值
不能定义数组
不能用于类的非常量静态成员变量的初始化
class A { public: auto c = 2;//报错 static auto a;//报错,因为静态成员变量需要在类外赋值 const
static auto b = 2; };
auto经常应用在STL容器的遍历,其他地方尽量少用,不利于代码阅读
增强for循环
在增强for循环中不需要传递容器需要遍历的范围,循环会自动以容器为范围展开,并且循环屏蔽掉了迭代器的遍历细节,直接抽取容器中的元素进行运算。
如果想要修改遍历的容器,则需要使用引用的方式遍历
如果只读数据,不修改元素的值for(const auto &it:vec)比非引用的效率高一些
注意遍历哈希表时获取的是对象不是迭代器所以要用it.first或者it.second