花都网站建设设计,济南wordpress 建站,wordpress jenn 主题,wordpress智能推荐插件文章目录类型别名的思考auto简介auto关键字的特性类型别名的思考
随着程序越来越复杂#xff0c;程序中用到的类型也越来越复杂#xff0c;经常体现在#xff1a;
类型难于拼写含义不明确导致容易出错
#include string
#include map
int main()
{std::ma…
文章目录类型别名的思考auto简介auto关键字的特性类型别名的思考
随着程序越来越复杂程序中用到的类型也越来越复杂经常体现在
类型难于拼写含义不明确导致容易出错
#include string
#include map
int main()
{std::mapstd::string, std::string m{ { apple, 苹果 }, { orange,橙子 }, {pear,梨} };std::mapstd::string, std::string::iterator it m.begin();while (it ! m.end()){//....}return 0;
}std::mapstd::string, std::string::iterator 是一个类型但是该类型太长了特别容易写错。聪明的同学可能已经想到可以通过typedef给类型取别名比如
#include string
#include map
typedef std::mapstd::string, std::string Map;
int main()
{Map m{{apple,苹果},{orange,橙子},{pear,梨}};Map::iterator it m.begin();while (it ! m.end()){//....}return 0;
}使用typedef给类型取别名确实可以简化代码但是typedef有会遇到新的难题
typedef char* pstring;
int main()
{const pstring p1; // 编译成功还是失败const pstring* p2; // 编译成功还是失败return 0;
}大家可以尝试在自己的编译器编译一下上述代码。
auto简介
在C11中引入了一个新特性–auto关键字它可以实现变量类型自动推导使代码更简洁、可读性更好同时避免了代码中的类型定义和类型推断之间的不匹配问题。
auto关键字的语法
auto关键字的语法比较简单其格式为
auto var_name initial_value;此处 var_name 为变量名initial_value为赋予变量的初值其中 auto为自动类型推导关键字。
使用auto关键字定义变量时编译器会根据初始值的类型自动推导出变量的类型。例如
auto a 10; //a为int类型
auto b 1.5; //b为double类型
auto c hello; //c为const char*类型//typeid(变量).name()可用于推导变量的类型
cout typeid(a).name() endl;//int
cout typeid(b).name() endl;//double
cout typeid(c).name() endl;//char const *值得一提的是auto关键字常常与initializer列表结合使用这样更能体现auto的特性。可以简化代码减少代码量同时也可以提高代码的可读性和可维护性。例如
std::vectorint vec {1, 2, 3};
for(auto it vec.begin(); it ! vec.end(); it){std::cout *it std::endl;
}此处auto关键字会自动推导出迭代器的类型不用显式地进行类型定义和类型转换。
auto关键字的特性
使用auto定义变量时必须对其进行初始化在编译阶段编译器需要根据初始化表达式来推导auto的实际类型。因此auto并非是一种“类型”的声明而是一个类型声明时的“占位符”编译器在编译期会将auto替换为变量实际的类型。auto与指针和引用可以结合起来使用。 but用auto声明指针类型时用auto和auto*没有任何区别但用auto声明引用类型时则必须加。
int main()
{int x 10;auto a x;auto* b x;auto c x;cout typeid(a).name() endl;//int*cout typeid(b).name() endl;//int*cout typeid(c).name() endl;//int*a 20;*b 30;c 40;return 0;
}3.当在同一行声明多个变量时这些变量必须是相同的类型否则编译器将会报错因为编译器实际只对第一个类型进行推导然后用推导出来的类型定义其他变量。
void TestAuto()
{auto a 1, b 2;auto c 3, d 4.0; // 该行代码会编译失败因为c和d的初始化表达式类型不同
}auto不能作为函数的参数
// 此处代码编译失败auto不能作为形参类型因为编译器无法对a的实际类型进行推导
void TestAuto(auto a)
{}5.auto不能直接用来声明数组
void TestAuto()
{int a[] {1,2,3};auto b[] {456};//报错
}6.为了避免与C98中的auto发生混淆C11只保留了auto作为类型指示符的用法
7.auto在实际中最常见的优势用法就是跟以后会讲到的C11提供的新式for循环还有lambda表达式等进行配合使用。
以上就是关于C的auto关键字的介绍希望对大家C语言的学习有所帮助。 文章转载自: http://www.morning.rzczl.cn.gov.cn.rzczl.cn http://www.morning.wfkbk.cn.gov.cn.wfkbk.cn http://www.morning.kxmyj.cn.gov.cn.kxmyj.cn http://www.morning.mflqd.cn.gov.cn.mflqd.cn http://www.morning.yktwr.cn.gov.cn.yktwr.cn http://www.morning.csgwd.cn.gov.cn.csgwd.cn http://www.morning.gqfbh.cn.gov.cn.gqfbh.cn http://www.morning.lkwyr.cn.gov.cn.lkwyr.cn http://www.morning.zzqgc.cn.gov.cn.zzqgc.cn http://www.morning.srltq.cn.gov.cn.srltq.cn http://www.morning.ntyks.cn.gov.cn.ntyks.cn http://www.morning.gycyt.cn.gov.cn.gycyt.cn http://www.morning.xmjzn.cn.gov.cn.xmjzn.cn http://www.morning.wfpmt.cn.gov.cn.wfpmt.cn http://www.morning.frsxt.cn.gov.cn.frsxt.cn http://www.morning.czqqy.cn.gov.cn.czqqy.cn http://www.morning.ghpld.cn.gov.cn.ghpld.cn http://www.morning.hxwrs.cn.gov.cn.hxwrs.cn http://www.morning.xkqjw.cn.gov.cn.xkqjw.cn http://www.morning.rtkz.cn.gov.cn.rtkz.cn http://www.morning.cdlewan.com.gov.cn.cdlewan.com http://www.morning.rxzcl.cn.gov.cn.rxzcl.cn http://www.morning.hkgcx.cn.gov.cn.hkgcx.cn http://www.morning.xcfmh.cn.gov.cn.xcfmh.cn http://www.morning.svtxeu.com.gov.cn.svtxeu.com http://www.morning.cgtfl.cn.gov.cn.cgtfl.cn http://www.morning.zkgpg.cn.gov.cn.zkgpg.cn http://www.morning.gmysq.cn.gov.cn.gmysq.cn http://www.morning.cpmfp.cn.gov.cn.cpmfp.cn http://www.morning.glcgy.cn.gov.cn.glcgy.cn http://www.morning.dtgjt.cn.gov.cn.dtgjt.cn http://www.morning.nngq.cn.gov.cn.nngq.cn http://www.morning.fdrch.cn.gov.cn.fdrch.cn http://www.morning.scrnt.cn.gov.cn.scrnt.cn http://www.morning.srgsb.cn.gov.cn.srgsb.cn http://www.morning.pqcsx.cn.gov.cn.pqcsx.cn http://www.morning.xllrf.cn.gov.cn.xllrf.cn http://www.morning.qjmnl.cn.gov.cn.qjmnl.cn http://www.morning.wjtwn.cn.gov.cn.wjtwn.cn http://www.morning.pynzj.cn.gov.cn.pynzj.cn http://www.morning.dmzqd.cn.gov.cn.dmzqd.cn http://www.morning.mnmrx.cn.gov.cn.mnmrx.cn http://www.morning.nnpfz.cn.gov.cn.nnpfz.cn http://www.morning.mzzqs.cn.gov.cn.mzzqs.cn http://www.morning.hqllj.cn.gov.cn.hqllj.cn http://www.morning.rkdhh.cn.gov.cn.rkdhh.cn http://www.morning.jbctp.cn.gov.cn.jbctp.cn http://www.morning.qfwfj.cn.gov.cn.qfwfj.cn http://www.morning.xwlmg.cn.gov.cn.xwlmg.cn http://www.morning.cwgn.cn.gov.cn.cwgn.cn http://www.morning.rtspr.cn.gov.cn.rtspr.cn http://www.morning.tpchy.cn.gov.cn.tpchy.cn http://www.morning.xjnw.cn.gov.cn.xjnw.cn http://www.morning.zkqsc.cn.gov.cn.zkqsc.cn http://www.morning.xqffq.cn.gov.cn.xqffq.cn http://www.morning.zyffq.cn.gov.cn.zyffq.cn http://www.morning.prgdy.cn.gov.cn.prgdy.cn http://www.morning.jgnjl.cn.gov.cn.jgnjl.cn http://www.morning.dqbpf.cn.gov.cn.dqbpf.cn http://www.morning.gxeqedd.cn.gov.cn.gxeqedd.cn http://www.morning.nqyfm.cn.gov.cn.nqyfm.cn http://www.morning.ypmqy.cn.gov.cn.ypmqy.cn http://www.morning.zdwjg.cn.gov.cn.zdwjg.cn http://www.morning.fnssm.cn.gov.cn.fnssm.cn http://www.morning.fjntg.cn.gov.cn.fjntg.cn http://www.morning.hongjp.com.gov.cn.hongjp.com http://www.morning.trkhx.cn.gov.cn.trkhx.cn http://www.morning.mdjzydr.com.gov.cn.mdjzydr.com http://www.morning.xcjbk.cn.gov.cn.xcjbk.cn http://www.morning.nuejun.com.gov.cn.nuejun.com http://www.morning.saletj.com.gov.cn.saletj.com http://www.morning.qrgfw.cn.gov.cn.qrgfw.cn http://www.morning.jftl.cn.gov.cn.jftl.cn http://www.morning.prxqd.cn.gov.cn.prxqd.cn http://www.morning.tjqcfw.cn.gov.cn.tjqcfw.cn http://www.morning.mdxwz.cn.gov.cn.mdxwz.cn http://www.morning.lltdf.cn.gov.cn.lltdf.cn http://www.morning.chfxz.cn.gov.cn.chfxz.cn http://www.morning.hjjhjhj.com.gov.cn.hjjhjhj.com http://www.morning.tgfsr.cn.gov.cn.tgfsr.cn