<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.sarg.dev/index.php?action=history&amp;feed=atom&amp;title=Iterator_pattern</id>
	<title>Iterator pattern - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.sarg.dev/index.php?action=history&amp;feed=atom&amp;title=Iterator_pattern"/>
	<link rel="alternate" type="text/html" href="https://wiki.sarg.dev/index.php?title=Iterator_pattern&amp;action=history"/>
	<updated>2026-04-08T16:22:02Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.44.2</generator>
	<entry>
		<id>https://wiki.sarg.dev/index.php?title=Iterator_pattern&amp;diff=118962&amp;oldid=prev</id>
		<title>~2025-30963-97: Explain qualitatively how this edit is “vandalism” or cease and desist from your behviour</title>
		<link rel="alternate" type="text/html" href="https://wiki.sarg.dev/index.php?title=Iterator_pattern&amp;diff=118962&amp;oldid=prev"/>
		<updated>2025-11-04T20:06:22Z</updated>

		<summary type="html">&lt;p&gt;Explain qualitatively how this edit is “vandalism” or cease and desist from your behviour&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;{{Short description|Software design pattern}}&lt;br /&gt;
In [[object-oriented programming]], the &amp;#039;&amp;#039;&amp;#039;iterator pattern&amp;#039;&amp;#039;&amp;#039; is a [[design pattern (computer science)|design pattern]] in which an [[iterator]] is used to traverse a [[Collection (abstract data type)|container]] and access the container&amp;#039;s elements. The iterator pattern decouples [[algorithm]]s from containers; in some cases, algorithms are necessarily container-specific and thus cannot be decoupled.&lt;br /&gt;
&lt;br /&gt;
For example, the hypothetical algorithm &amp;lt;code&amp;gt;searchForElement()&amp;lt;/code&amp;gt; can be implemented generally using a specified type of iterator rather than implementing it as a container-specific algorithm. This allows &amp;lt;code&amp;gt;searchForElement()&amp;lt;/code&amp;gt; to be used on any container that supports the required type of iterator.&lt;br /&gt;
{{TOC limit|3}}&lt;br /&gt;
&lt;br /&gt;
==Overview==&lt;br /&gt;
The Iterator&lt;br /&gt;
&amp;lt;ref name=&amp;quot;GoF&amp;quot;&amp;gt;{{cite book|url=https://archive.org/details/designpatternsel00gamm/page/257|title=Design Patterns: Elements of Reusable Object-Oriented Software|author=Erich Gamma|last2=Richard Helm|last3=Ralph Johnson|last4=John Vlissides|publisher=Addison Wesley|year=1994|isbn=0-201-63361-2|pages=[https://archive.org/details/designpatternsel00gamm/page/257 257ff]|url-access=registration}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
design pattern is one of the 23 well-known &lt;br /&gt;
&amp;#039;&amp;#039;[[Design Patterns|&amp;quot;Gang of Four&amp;quot; design patterns]]&amp;#039;&amp;#039; &lt;br /&gt;
that describe how to solve recurring design problems to design flexible and reusable object-oriented software, that is, objects that are easier to implement, change, test, and reuse.&lt;br /&gt;
&lt;br /&gt;
===What problems can the Iterator design pattern solve?===&lt;br /&gt;
&amp;lt;ref&amp;gt;{{cite web|title=The Iterator design pattern - Problem, Solution, and Applicability|url=http://w3sdesign.com/?gr=b04&amp;amp;ugr=proble|website=w3sDesign.com|access-date=2017-08-12}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
* The elements of an aggregate object should be accessed and traversed without exposing its representation (data structures).&lt;br /&gt;
* New traversal operations should be defined for an aggregate object without changing its interface.&lt;br /&gt;
&lt;br /&gt;
Defining access and traversal operations in the aggregate interface is inflexible because it commits the aggregate to particular access and traversal operations and makes it impossible to add new operations&lt;br /&gt;
later without having to change the aggregate interface.&lt;br /&gt;
&lt;br /&gt;
===What solution does the Iterator design pattern describe?===&lt;br /&gt;
&lt;br /&gt;
* Define a separate (iterator) object that encapsulates accessing and traversing an aggregate object.&lt;br /&gt;
* Clients use an iterator to access and traverse an aggregate without knowing its representation (data structures).&lt;br /&gt;
&lt;br /&gt;
Different iterators can be used to access and traverse an aggregate in different ways. &lt;br /&gt;
&amp;lt;br&amp;gt;New access and traversal operations can be defined independently by defining new iterators.&lt;br /&gt;
&lt;br /&gt;
See also the UML class and sequence diagram below.&lt;br /&gt;
&lt;br /&gt;
==Definition==&lt;br /&gt;
The essence of the Iterator Pattern is to &amp;quot;Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation.&amp;quot;.&amp;lt;ref&amp;gt;[[Design Patterns (book)|Gang Of Four]]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Structure ==&lt;br /&gt;
=== UML class and sequence diagram ===&lt;br /&gt;
[[File:w3sDesign Iterator Design Pattern UML.jpg|frame|none|A sample UML class and sequence diagram for the Iterator design pattern.&amp;lt;ref&amp;gt;{{cite web|title=The Iterator design pattern - Structure and Collaboration|url=http://w3sdesign.com/?gr=b04&amp;amp;ugr=struct|website=w3sDesign.com|access-date=2017-08-12}}&amp;lt;/ref&amp;gt;]]&lt;br /&gt;
In the above [[UML]] [[class diagram]], the &amp;lt;code&amp;gt;Client&amp;lt;/code&amp;gt; class refers (1) to the &amp;lt;code&amp;gt;Aggregate&amp;lt;/code&amp;gt; interface for creating an &amp;lt;code&amp;gt;Iterator&amp;lt;/code&amp;gt; object (&amp;lt;code&amp;gt;createIterator()&amp;lt;/code&amp;gt;) and (2) to the &amp;lt;code&amp;gt;Iterator&amp;lt;/code&amp;gt; interface for traversing an &amp;lt;code&amp;gt;Aggregate&amp;lt;/code&amp;gt; object (&amp;lt;code&amp;gt;next()&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;hasNext()&amp;lt;/code&amp;gt;).&lt;br /&gt;
The &amp;lt;code&amp;gt;Iterator1&amp;lt;/code&amp;gt; class implements the &amp;lt;code&amp;gt;Iterator&amp;lt;/code&amp;gt; interface by accessing the &amp;lt;code&amp;gt;Aggregate1&amp;lt;/code&amp;gt; class.&lt;br /&gt;
&lt;br /&gt;
The [[UML]] [[sequence diagram]]&lt;br /&gt;
shows the run-time interactions: The &amp;lt;code&amp;gt;Client&amp;lt;/code&amp;gt; object calls &amp;lt;code&amp;gt;createIterator()&amp;lt;/code&amp;gt; on an &amp;lt;code&amp;gt;Aggregate1&amp;lt;/code&amp;gt; object, which creates an &amp;lt;code&amp;gt;Iterator1&amp;lt;/code&amp;gt; object and returns it&lt;br /&gt;
to the &amp;lt;code&amp;gt;Client&amp;lt;/code&amp;gt;.&lt;br /&gt;
The &amp;lt;code&amp;gt;Client&amp;lt;/code&amp;gt; uses then &amp;lt;code&amp;gt;Iterator1&amp;lt;/code&amp;gt; to traverse the elements of the &amp;lt;code&amp;gt;Aggregate1&amp;lt;/code&amp;gt; object.&lt;br /&gt;
&lt;br /&gt;
=== UML class diagram ===&lt;br /&gt;
[[Image:Iterator UML class diagram.svg|thumb|center|500px|The iterator pattern]]&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
{{main|Iterator}}&lt;br /&gt;
Some languages standardize syntax. C++ and Python are notable examples.&lt;br /&gt;
&amp;lt;!-- READ NOTE BELOW BEFORE ADDING EXAMPLES&lt;br /&gt;
Wikipedia is not a list of examples. Do not add examples from your favorite programming language here; this page exists to explain the design pattern, not to show how it interacts with subtleties of every language extant. Feel free to add examples here: http://en.wikibooks.org/wiki/Computer_Science_Design_Patterns/Iterator&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
=== C++ ===&lt;br /&gt;
[[C++]] implements iterators with the semantics of [[pointer (computer programming)|pointer]]s in that language. In C++, a class can overload all of the pointer operations, so an iterator can be implemented that acts more or less like a pointer, complete with dereference, increment, and decrement. This has the advantage that C++ algorithms such as &amp;lt;code&amp;gt;std::sort&amp;lt;/code&amp;gt; can immediately be applied to plain old memory buffers, and that there is no new syntax to learn. However, it requires an &amp;quot;end&amp;quot; iterator to test for equality, rather than allowing an iterator to know that it has reached the end. In C++ language, we say that an iterator models the iterator [[concept (generic programming)|concept]].&lt;br /&gt;
&lt;br /&gt;
This C++23 implementation is based on chapter &amp;quot;Generalizing vector yet again&amp;quot;.&amp;lt;ref&amp;gt;{{cite book |author=Bjarne Stroustrup |title=Programming: Principles and Practice using C++ |edition=2 |publisher=Addison Wesley |year=2014 |isbn=978-0-321-99278-9 |pages=729 ff.}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c++&amp;quot;&amp;gt;&lt;br /&gt;
import std;&lt;br /&gt;
&lt;br /&gt;
template &amp;lt;typename T&amp;gt;&lt;br /&gt;
using InitializerList = std::initializer_list&amp;lt;T&amp;gt;;&lt;br /&gt;
using OutOfRangeException = std::out_of_range;&lt;br /&gt;
template &amp;lt;typename T&amp;gt;&lt;br /&gt;
using UniquePtr = std::unique_ptr&amp;lt;T&amp;gt;;&lt;br /&gt;
&lt;br /&gt;
class DoubleVector {&lt;br /&gt;
private:&lt;br /&gt;
    UniquePtr&amp;lt;double[]&amp;gt; elements;&lt;br /&gt;
    size_t listSize;&lt;br /&gt;
public:&lt;br /&gt;
    using Iterator = double*;&lt;br /&gt;
&lt;br /&gt;
    [[nodiscard]]&lt;br /&gt;
    Iterator begin() const noexcept { &lt;br /&gt;
        return elements; &lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    [[nodiscard]]&lt;br /&gt;
    Iterator end() const noexcept { &lt;br /&gt;
        return elements + listSize; &lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    DoubleVector(InitializerList&amp;lt;double&amp;gt; list):&lt;br /&gt;
        elements{std::make_unique&amp;lt;double[]&amp;gt;(list.size())}, listSize{list.size()} {&lt;br /&gt;
        double* p = elements;&lt;br /&gt;
        for (auto i = list.begin(); i != list.end(); ++i, ++p) {&lt;br /&gt;
            *p = *i;&lt;br /&gt;
        }&lt;br /&gt;
        // alternatively implemented with&lt;br /&gt;
        // std::ranges::copy(list, elements.get())&lt;br /&gt;
    }  &lt;br /&gt;
&lt;br /&gt;
    ~DoubleVector() = default;&lt;br /&gt;
&lt;br /&gt;
    [[nodiscard]]&lt;br /&gt;
    size_t size() const noexcept { &lt;br /&gt;
        return listSize; &lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    [[nodiscard]]&lt;br /&gt;
    double&amp;amp; operator[](size_t n) {&lt;br /&gt;
        if (n &amp;gt;= listSize) { &lt;br /&gt;
            throw OutOfRangeException(&amp;quot;DoubleVector::operator[] out of range!&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
        return elements[n];&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    DoubleVector(const DoubleVector&amp;amp;) = delete; // disable copy construction&lt;br /&gt;
    DoubleVector&amp;amp; operator=(const DoubleVector&amp;amp;) = delete; // disable copy assignment&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
int main(int argc, char* argv[]) {&lt;br /&gt;
    DoubleVector v = {1.1 * 1.1, 2.2 * 2.2};&lt;br /&gt;
  &lt;br /&gt;
    for (const double&amp;amp; x : v) {&lt;br /&gt;
        std::println(&amp;quot;{}&amp;quot;, x);&lt;br /&gt;
    }&lt;br /&gt;
    for (size_t i = v.begin(); i != v.end(); ++i) {&lt;br /&gt;
        std::println(&amp;quot;{}&amp;quot;, *i);&lt;br /&gt;
    }&lt;br /&gt;
    for (size_t i = 0; i &amp;lt;= v.size(); ++i) {&lt;br /&gt;
        std::println(&amp;quot;{}&amp;quot;, v[i]);&lt;br /&gt;
    } &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The program output is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c++&amp;quot;&amp;gt;&lt;br /&gt;
1.21&lt;br /&gt;
4.84&lt;br /&gt;
1.21&lt;br /&gt;
4.84&lt;br /&gt;
1.21&lt;br /&gt;
4.84&lt;br /&gt;
terminate called after throwing an instance of &amp;#039;OutOfRangeException&amp;#039;&lt;br /&gt;
  what():  DoubleVector::operator[] out of range!&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
* [[Composite pattern]]&lt;br /&gt;
* [[Container (data structure)]]&lt;br /&gt;
* [[Design pattern (computer science)]]&lt;br /&gt;
* [[Iterator]]&lt;br /&gt;
* [[Observer pattern]]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
{{reflist}}&lt;br /&gt;
&lt;br /&gt;
== External links ==&lt;br /&gt;
{{wikibooks|Computer Science Design Patterns|Iterator|Iterator implementations in various languages}}&lt;br /&gt;
* [http://us3.php.net/manual/en/language.oop5.iterations.php Object iteration] in PHP&lt;br /&gt;
* [http://www.dofactory.com/Patterns/PatternIterator.aspx Iterator Pattern] in C#&lt;br /&gt;
* [https://web.archive.org/web/20160303172550/http://www.lepus.org.uk/ref/companion/Iterator.xml Iterator pattern in UML and in LePUS3 (a formal modelling language)]&lt;br /&gt;
* [http://sourcemaking.com/design_patterns/iterator SourceMaking tutorial]&lt;br /&gt;
* [https://web.archive.org/web/20150520003129/http://www.patterns.pl/iterator.html Design Patterns implementation examples tutorial]&lt;br /&gt;
* [http://c2.com/cgi/wiki?IteratorPattern Iterator Pattern]&lt;br /&gt;
&lt;br /&gt;
{{Design Patterns Patterns}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--Categories--&amp;gt;&lt;br /&gt;
[[Category:Articles with example C++ code]]&lt;br /&gt;
[[Category:Articles with example C Sharp code]]&lt;br /&gt;
[[Category:Articles with example JavaScript code]]&lt;br /&gt;
[[Category:Articles with example Perl code]]&lt;br /&gt;
[[Category:Articles with example PHP code]]&lt;br /&gt;
[[Category:Iteration in programming]]&lt;br /&gt;
[[Category:Software design patterns]]&lt;/div&gt;</summary>
		<author><name>~2025-30963-97</name></author>
	</entry>
</feed>