在数组索引上使用迭代器 [英] Use of iterators over array indices

查看:145
本文介绍了在数组索引上使用迭代器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想知道使用迭代器对数组索引的主要优点是什么。我已经google了,但我得不到正确的答案。

I just wanted to know what is the main advantage of using the iterators over the array indices. I have googled but i am not getting the right answer.

推荐答案

我推测你在使用矢量,

I presume you are talking about when using a vector, right?

主要的优点是迭代器代码适用于所有stl容器,而数组索引操作符 [] 可用于向量和deques。这意味着您可以自由更改底层容器,如果你需要,而不必重新编码每个循环。这也意味着你可以将你的迭代代码放在一个模板中,它将适用于任何容器,不只是对于deques和向量(和数组当然)。

The main advantage is that iterator code works for all stl containers, while the array indexing operator [] is only available for vectors and deques. This means you are free to change the underlying container if you need to without having to recode every loop. It also means you can put your iteration code in a template and it will work for any container, not just for deques and vectors (and arrays of course).

这篇关于在数组索引上使用迭代器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆