使用迭代器的accesing向量元素和索引之间的区别是什么? [英] What is the difference between accesing vector elements using an iterator vs an index?
本文介绍了使用迭代器的accesing向量元素和索引之间的区别是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用迭代器和索引访问向量元素有什么优点?
What advantages are there in accessing vector elements using an iterator vs an index?
推荐答案
为什么迭代器比索引更好?
Why are iterators better than indexes?
- 在索引不可用的情况下(例如std :: list,对于
示例)。 - 在调用接受迭代器
的泛型函数的情况下。 - 在编写一个应该使用
的函数模板时多个容器类型。 - 它们用于在所有容器中创建一致性,并且能够使用
所有容器的迭代器以及常规指针所有标准
算法。 - 迭代器可以指向除了作为概念之外不存在的序列。
例如,你可以创建一个迭代器类,逐步通过素数
数字,而不必实际构建一个素数容器。
- In the cases where index is not available (like with std::list, for example).
- In the case where a generic function accepting an iterator is called.
- When writing a function template that is supposed to work with more than one container type.
- They exist to create uniformity among all containers and ability to use all containers' iterators as well as regular pointers in all standard algorithms.
- Iterators can point to sequences that don't exist except as a concept. For instance, you can make an iterator class that steps through prime numbers without actually having to build a container of primes.
但是,如果忽略不支持随机访问(列表,集合等)的容器类型,迭代器仍然会提供
However, if ignoring container types that do not support random access (list, set, etc.), iterators still offer
- 泛化的概念可以超越对
容器内的元素的迭代。 - 在少数情况下性能优于容器成员函数。
这篇关于使用迭代器的accesing向量元素和索引之间的区别是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文