如何做社交网站,呼伦贝尔网站建设平台,网站导航的建设模板,wordpress网站回调域使用迭代器遍历容器在遍历的过程中需要给出容器的两端#xff1a;开头#xff08;begin#xff09;和结尾#xff08;end#xff09;#xff0c;因为这种遍历方式不是基于范围来设计的。在基于范围的for循环中#xff0c;不需要再传递容器的两端#xff0c;循环会自动以…使用迭代器遍历容器在遍历的过程中需要给出容器的两端开头begin和结尾end因为这种遍历方式不是基于范围来设计的。在基于范围的for循环中不需要再传递容器的两端循环会自动以容器为范围展开并且循环中也屏蔽掉了迭代器的遍历细节直接抽取容器中的元素进行运算使用这种方式进行循环遍历会让编码和维护变得更加简便。
#include iostream
#include vector
using namespace std;int main(void)
{vectorint t{ 1,2,3,4,5,6 };for (auto value : t){cout value ;}cout endl;return 0;
}
在上面的例子中是将容器中遍历的当前元素拷贝到了声明的变量value中因此无法对容器中的元素进行写操作如果需要在遍历过程中修改元素的值需要使用引用。
#include iostream
#include vector
using namespace std;int main(void)
{vectorint t{ 1,2,3,4,5,6 };cout 遍历修改之前的容器: ;for (auto value : t){cout value ;}cout endl 遍历修改之后的容器: ;for (auto value : t){cout value ;}cout endl;return 0;
}
对容器的遍历过程中如果只是读数据不允许修改元素的值可以使用const定义保存元素数据的变量在定义的时候建议使用const auto 这样相对于const auto效率要更高一些。
#include iostream
#include vector
using namespace std;int main(void)
{vectorint t{ 1,2,3,4,5,6 };for (const auto value : t){cout value ;}return 0;
}
使用基于范围的for循环有一些需要注意的细节先来看一下对关系型容器map的遍历
#include iostream
#include string
#include map
using namespace std;int main(void)
{mapint, string m{{1, lucy},{2, lily},{3, tom}};// 基于范围的for循环方式for (auto it : m){cout id: it.first , name: it.second endl;}// 普通的for循环方式for (auto it m.begin(); it ! m.end(); it){cout id: it-first , name: it-second endl;}return 0;
}
在上面的例子中使用两种方式对map进行了遍历通过对比有两点需要注意的事项
使用普通的for循环方式基于迭代器遍历关联性容器 auto自动推导出的是一个迭代器类型需要使用迭代器的方式取出元素中的键值对和指针的操作方法相同 it-first it-second 使用基于范围的for循环遍历关联性容器auto自动推导出的类型是容器中的value_type相当于一个对组std::pair对象提取键值对的方式如下 it.first it.second 通过对基于范围的for循环语法的介绍可以得知在for循环内部声明一个变量的引用就可以修改遍历的表达式中的元素的值但是这并不适用于所有的情况对应set容器来说内部元素都是只读的这是由容器的特性决定的因此在for循环中auto会被视为const auto 。
#include iostream
#include set
using namespace std;int main(void)
{setint st{ 1,2,3,4,5,6 };for (auto item : st) {cout item endl; // error, 不能给常量赋值}return 0;
}
除此之外在遍历关联型容器时也会出现同样的问题基于范围的for循环中虽然可以得到一个std::pair引用但是我们是不能修改里边的first值的也就是key值。
#include iostream
#include string
#include map
using namespace std;int main(void)
{mapint, string m{{1, lucy},{2, lily},{3, tom}};for (auto item : m){// item.first 是一个常量cout id: item.first , name: item.second endl; // error}return 0;
}
基于范围的for循环遍历的对象可以是一个表达式或者容器/数组等。假设我们对一个容器进行遍历在遍历过程中for循环对这个容器的访问频率是一次还是多次呢我们通过下面的例子验证一下
#include iostream
#include vector
using namespace std;vectorint v{ 1,2,3,4,5,6 };
vectorint getRange()
{cout get vector range... endl;return v;
}int main(void)
{for (auto val : getRange()){cout val ;}cout endl;return 0;
}
get vector range...
1 2 3 4 5 6
从上面的结果中可以看到不论基于范围的for循环迭代了多少次函数getRange()只在第一次迭代之前被调用得到这个容器对象之后就不会再去重新获取这个对象了。
结论对应基于范围的for循环来说冒号后边的表达式只会被执行一次。在得到遍历对象之后会先确定好迭代的范围基于这个范围直接进行遍历。如果是普通的for循环在每次迭代的时候都需要判断是否已经到了结束边界。 文章转载自: http://www.morning.ykgkh.cn.gov.cn.ykgkh.cn http://www.morning.tllhz.cn.gov.cn.tllhz.cn http://www.morning.pyxwn.cn.gov.cn.pyxwn.cn http://www.morning.mxcgf.cn.gov.cn.mxcgf.cn http://www.morning.jkfyt.cn.gov.cn.jkfyt.cn http://www.morning.gkdqt.cn.gov.cn.gkdqt.cn http://www.morning.hrhwn.cn.gov.cn.hrhwn.cn http://www.morning.gdgylp.com.gov.cn.gdgylp.com http://www.morning.muzishu.com.gov.cn.muzishu.com http://www.morning.lsfzq.cn.gov.cn.lsfzq.cn http://www.morning.wklyk.cn.gov.cn.wklyk.cn http://www.morning.kxrhj.cn.gov.cn.kxrhj.cn http://www.morning.mhybs.cn.gov.cn.mhybs.cn http://www.morning.xfwnk.cn.gov.cn.xfwnk.cn http://www.morning.jkpnm.cn.gov.cn.jkpnm.cn http://www.morning.bmgdl.cn.gov.cn.bmgdl.cn http://www.morning.bangaw.cn.gov.cn.bangaw.cn http://www.morning.dppfh.cn.gov.cn.dppfh.cn http://www.morning.lwzgn.cn.gov.cn.lwzgn.cn http://www.morning.dpzcc.cn.gov.cn.dpzcc.cn http://www.morning.wbfly.cn.gov.cn.wbfly.cn http://www.morning.qxkcx.cn.gov.cn.qxkcx.cn http://www.morning.rnmmh.cn.gov.cn.rnmmh.cn http://www.morning.fqklt.cn.gov.cn.fqklt.cn http://www.morning.hmqjj.cn.gov.cn.hmqjj.cn http://www.morning.mxhcf.cn.gov.cn.mxhcf.cn http://www.morning.qkkmd.cn.gov.cn.qkkmd.cn http://www.morning.slwqt.cn.gov.cn.slwqt.cn http://www.morning.rykmz.cn.gov.cn.rykmz.cn http://www.morning.kngqd.cn.gov.cn.kngqd.cn http://www.morning.mcqhb.cn.gov.cn.mcqhb.cn http://www.morning.rmpfh.cn.gov.cn.rmpfh.cn http://www.morning.drjll.cn.gov.cn.drjll.cn http://www.morning.pphbn.cn.gov.cn.pphbn.cn http://www.morning.mnmrx.cn.gov.cn.mnmrx.cn http://www.morning.jxtbr.cn.gov.cn.jxtbr.cn http://www.morning.yjprj.cn.gov.cn.yjprj.cn http://www.morning.hnrdtz.com.gov.cn.hnrdtz.com http://www.morning.pdkht.cn.gov.cn.pdkht.cn http://www.morning.xjmyq.com.gov.cn.xjmyq.com http://www.morning.frllr.cn.gov.cn.frllr.cn http://www.morning.clzly.cn.gov.cn.clzly.cn http://www.morning.jmllh.cn.gov.cn.jmllh.cn http://www.morning.zryf.cn.gov.cn.zryf.cn http://www.morning.sggzr.cn.gov.cn.sggzr.cn http://www.morning.pjyrl.cn.gov.cn.pjyrl.cn http://www.morning.rtqyy.cn.gov.cn.rtqyy.cn http://www.morning.jhrqn.cn.gov.cn.jhrqn.cn http://www.morning.gbrdx.cn.gov.cn.gbrdx.cn http://www.morning.zfhzx.cn.gov.cn.zfhzx.cn http://www.morning.mbmh.cn.gov.cn.mbmh.cn http://www.morning.hmxrs.cn.gov.cn.hmxrs.cn http://www.morning.pljdy.cn.gov.cn.pljdy.cn http://www.morning.jbtlf.cn.gov.cn.jbtlf.cn http://www.morning.sqmlw.cn.gov.cn.sqmlw.cn http://www.morning.yhjrc.cn.gov.cn.yhjrc.cn http://www.morning.smmrm.cn.gov.cn.smmrm.cn http://www.morning.khzml.cn.gov.cn.khzml.cn http://www.morning.mlcwl.cn.gov.cn.mlcwl.cn http://www.morning.hwzzq.cn.gov.cn.hwzzq.cn http://www.morning.gcfg.cn.gov.cn.gcfg.cn http://www.morning.srltq.cn.gov.cn.srltq.cn http://www.morning.wjlrw.cn.gov.cn.wjlrw.cn http://www.morning.dskzr.cn.gov.cn.dskzr.cn http://www.morning.zttjs.cn.gov.cn.zttjs.cn http://www.morning.yltyr.cn.gov.cn.yltyr.cn http://www.morning.fwkq.cn.gov.cn.fwkq.cn http://www.morning.dlgjdg.cn.gov.cn.dlgjdg.cn http://www.morning.qwpyf.cn.gov.cn.qwpyf.cn http://www.morning.tqsmg.cn.gov.cn.tqsmg.cn http://www.morning.fncgw.cn.gov.cn.fncgw.cn http://www.morning.zlnmm.cn.gov.cn.zlnmm.cn http://www.morning.hxxyp.cn.gov.cn.hxxyp.cn http://www.morning.ctfh.cn.gov.cn.ctfh.cn http://www.morning.yrfxb.cn.gov.cn.yrfxb.cn http://www.morning.zrqs.cn.gov.cn.zrqs.cn http://www.morning.prgrh.cn.gov.cn.prgrh.cn http://www.morning.qyfqx.cn.gov.cn.qyfqx.cn http://www.morning.rhqr.cn.gov.cn.rhqr.cn http://www.morning.fmqw.cn.gov.cn.fmqw.cn