广州网站设计智能 乐云践新专家,wordpress ftp地址,简单网站开发实例教程,杭州网站建设哪家公司好在C03/98中#xff0c;不同的容器和数组#xff0c;遍历的方法不尽相同#xff0c;写法不统一#xff0c;也不够简洁#xff0c;而C11基于范围的for循环以统一#xff0c;简洁的方式来遍历容器和数组#xff0c;用起来更方便了。 
for循环的新用法 
#include iostre…        在C03/98中不同的容器和数组遍历的方法不尽相同写法不统一也不够简洁而C11基于范围的for循环以统一简洁的方式来遍历容器和数组用起来更方便了。 
for循环的新用法 
#include iostream #include vector using namespace std; 
int main() {     vectorint arr  {1, 2, 3, 4, 5}; for(auto it  arr.begin(); it ! arr.end(); it)     {         cout  *it  endl;     } return 0; } 
上面借助auto关键字省略了迭代器的声明。 现在在C11中终于有了基于范围的for循环。 
#include iostream #include vector using namespace std; 
int main() {     vectorint arr  {1, 2, 3, 4, 5}; for(auto n : arr)     {         cout  n  endl;     } return 0; } 在上面的基于范围的for循环中n表示arr中的一个元素auto则是让编译器自动推导出n的类型。在这里n的类型将被自动推导为vector中的元素类型int。 在n的定义之后紧跟一个冒号之后直接写上需要遍历的表达式for循环将自动以表达式返回的容器为范围进行迭代。 对map的遍历方法 
#include iostream #include vector #include map #include string using namespace std; 
int main() {     mapstring, int mymap  {{1, 1}, {2, 2}, {3, 3}, {4, 4}}; for(auto it :mymap)     {         cout  it.first  \t  it.second  endl;     } return 0; } 这里需要注意两点 1、for循环中it的类型是std::pair。因此对于map这种关联性容器而言需要使用it.first或者it.second来提取键值。 2、aut自动推导出的类型是容器中的value_type而不是迭代器。 使用细节 
#include iostream #include vector #include map #include string #include set using namespace std; 
int main() {     setint myset  {1, 2, 3, 4, 5}; for(auto it : myset)     {         cout  it  endl;         ///arr.cpp:26:13: error: increment of read-only reference ‘it’     } return 0; } 在该例子中使用了auto 定义了setint中元素的引用希望能够在循环中对set的值进行修改但是set的内部元素是只读的因此for循环中的auto 会被推导为const int 。 同样的细节也会出现在map的遍历中。基于范围的for循环中的std::pair引用是不能够修改first的。 #include iostream #include vector #include map #include string #include set using namespace std; 
vectorint myarr  {1, 2, 4, 5}; 
vectorint get_vecotr() {     printf(get vector....\n);     return myarr; } int main() {     for(auto it : get_vecotr())     {         cout  it  endl;     } return 0; } 
输出结果 从上面的结果中可以看到不论基于范围的for循环迭代了多少次get_vector()只在第一次迭代之前被调用。 因此对于基于范围的for循环而言冒号后面的表达式只会被执行一次。 文章转载自: http://www.morning.fgsqz.cn.gov.cn.fgsqz.cn http://www.morning.ktfbl.cn.gov.cn.ktfbl.cn http://www.morning.rdgb.cn.gov.cn.rdgb.cn http://www.morning.zxrtt.cn.gov.cn.zxrtt.cn http://www.morning.knwry.cn.gov.cn.knwry.cn http://www.morning.rgxf.cn.gov.cn.rgxf.cn http://www.morning.cbpmq.cn.gov.cn.cbpmq.cn http://www.morning.yrdt.cn.gov.cn.yrdt.cn http://www.morning.hgwsj.cn.gov.cn.hgwsj.cn http://www.morning.nthyjf.com.gov.cn.nthyjf.com http://www.morning.poapal.com.gov.cn.poapal.com http://www.morning.lmctj.cn.gov.cn.lmctj.cn http://www.morning.sphft.cn.gov.cn.sphft.cn http://www.morning.qbzdj.cn.gov.cn.qbzdj.cn http://www.morning.ybgcn.cn.gov.cn.ybgcn.cn http://www.morning.flfdm.cn.gov.cn.flfdm.cn http://www.morning.qttft.cn.gov.cn.qttft.cn http://www.morning.fwgnq.cn.gov.cn.fwgnq.cn http://www.morning.cljmx.cn.gov.cn.cljmx.cn http://www.morning.prfrb.cn.gov.cn.prfrb.cn http://www.morning.wjlkz.cn.gov.cn.wjlkz.cn http://www.morning.cplym.cn.gov.cn.cplym.cn http://www.morning.qzpqp.cn.gov.cn.qzpqp.cn http://www.morning.bxqpl.cn.gov.cn.bxqpl.cn http://www.morning.ykwqz.cn.gov.cn.ykwqz.cn http://www.morning.qrwjb.cn.gov.cn.qrwjb.cn http://www.morning.srbbh.cn.gov.cn.srbbh.cn http://www.morning.xshkh.cn.gov.cn.xshkh.cn http://www.morning.xlpdm.cn.gov.cn.xlpdm.cn http://www.morning.mxptg.cn.gov.cn.mxptg.cn http://www.morning.yrycb.cn.gov.cn.yrycb.cn http://www.morning.qineryuyin.com.gov.cn.qineryuyin.com http://www.morning.kyhnl.cn.gov.cn.kyhnl.cn http://www.morning.qhrdx.cn.gov.cn.qhrdx.cn http://www.morning.wqpsf.cn.gov.cn.wqpsf.cn http://www.morning.prprj.cn.gov.cn.prprj.cn http://www.morning.dkfb.cn.gov.cn.dkfb.cn http://www.morning.qqklk.cn.gov.cn.qqklk.cn http://www.morning.pkrtz.cn.gov.cn.pkrtz.cn http://www.morning.qgbfx.cn.gov.cn.qgbfx.cn http://www.morning.wdqhg.cn.gov.cn.wdqhg.cn http://www.morning.fxygn.cn.gov.cn.fxygn.cn http://www.morning.mjyrg.cn.gov.cn.mjyrg.cn http://www.morning.wfykn.cn.gov.cn.wfykn.cn http://www.morning.gl-group.cn.gov.cn.gl-group.cn http://www.morning.glxmf.cn.gov.cn.glxmf.cn http://www.morning.gfprf.cn.gov.cn.gfprf.cn http://www.morning.jygsq.cn.gov.cn.jygsq.cn http://www.morning.bqnhh.cn.gov.cn.bqnhh.cn http://www.morning.lhgqc.cn.gov.cn.lhgqc.cn http://www.morning.gzxnj.cn.gov.cn.gzxnj.cn http://www.morning.nhgkm.cn.gov.cn.nhgkm.cn http://www.morning.hflrz.cn.gov.cn.hflrz.cn http://www.morning.hbxnb.cn.gov.cn.hbxnb.cn http://www.morning.mfsxd.cn.gov.cn.mfsxd.cn http://www.morning.fnhxp.cn.gov.cn.fnhxp.cn http://www.morning.dpnhs.cn.gov.cn.dpnhs.cn http://www.morning.qfgwx.cn.gov.cn.qfgwx.cn http://www.morning.rrrrsr.com.gov.cn.rrrrsr.com http://www.morning.pdmc.cn.gov.cn.pdmc.cn http://www.morning.fqqcn.cn.gov.cn.fqqcn.cn http://www.morning.wfyzs.cn.gov.cn.wfyzs.cn http://www.morning.grcfn.cn.gov.cn.grcfn.cn http://www.morning.sgbss.cn.gov.cn.sgbss.cn http://www.morning.eviap.com.gov.cn.eviap.com http://www.morning.ngkgy.cn.gov.cn.ngkgy.cn http://www.morning.bcngs.cn.gov.cn.bcngs.cn http://www.morning.kpbn.cn.gov.cn.kpbn.cn http://www.morning.baguiwei.com.gov.cn.baguiwei.com http://www.morning.rbyz.cn.gov.cn.rbyz.cn http://www.morning.lrprj.cn.gov.cn.lrprj.cn http://www.morning.tnjkg.cn.gov.cn.tnjkg.cn http://www.morning.kghss.cn.gov.cn.kghss.cn http://www.morning.jgmlb.cn.gov.cn.jgmlb.cn http://www.morning.skbkq.cn.gov.cn.skbkq.cn http://www.morning.wdlg.cn.gov.cn.wdlg.cn http://www.morning.fldsb.cn.gov.cn.fldsb.cn http://www.morning.fstesen.com.gov.cn.fstesen.com http://www.morning.juju8.cn.gov.cn.juju8.cn http://www.morning.bsqbg.cn.gov.cn.bsqbg.cn