STL迭代器 - 目的 [英] STL iterators - purpose
问题描述
STL迭代器的用途是什么?程序员为什么要创建这个概念?
What is the purpose of STL iterators? Why have programmers created this concept at all?
推荐答案
迭代器允许您将算法与容器分开。只要你有一个开始和结束迭代器,并且知道迭代器的功能(随机访问等),你就可以在迭代器指定的范围内进行操作。例如,请参阅 std :: for_each
, std :: transform
。这样您就可以只实现一个算法版本,而不需要为每个希望它运行的容器编写一个新版本。
Iterators allow you to separate algorithms from the container. As long as you have a start and end iterator, and know the capabilities of the iterator (random access etc) you can do operations on the range specified by the iterators. For example, see std::for_each
, std::transform
. This way you can implement just one version of an algorithm rather than needing to write a new one for each container on which you wish it to operate.
这篇关于STL迭代器 - 目的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!