<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>http://debianws.lexgopc.com/wiki143/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="http://debianws.lexgopc.com/wiki143/index.php?action=history&amp;feed=atom&amp;title=Iterator_pattern"/>
	<link rel="alternate" type="text/html" href="http://debianws.lexgopc.com/wiki143/index.php?title=Iterator_pattern&amp;action=history"/>
	<updated>2026-10-25T22:53:33Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.43.1</generator>
	<entry>
		<id>http://debianws.lexgopc.com/wiki143/index.php?title=Iterator_pattern&amp;diff=3425655&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="http://debianws.lexgopc.com/wiki143/index.php?title=Iterator_pattern&amp;diff=3425655&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;a href=&quot;http://debianws.lexgopc.com/wiki143/index.php?title=Iterator_pattern&amp;amp;diff=3425655&amp;amp;oldid=119284&quot;&gt;Show changes&lt;/a&gt;</summary>
		<author><name>~2025-30963-97</name></author>
	</entry>
	<entry>
		<id>http://debianws.lexgopc.com/wiki143/index.php?title=Iterator_pattern&amp;diff=119284&amp;oldid=prev</id>
		<title>imported&gt;Tc14Hd: Added short description</title>
		<link rel="alternate" type="text/html" href="http://debianws.lexgopc.com/wiki143/index.php?title=Iterator_pattern&amp;diff=119284&amp;oldid=prev"/>
		<updated>2024-09-16T19:37:49Z</updated>

		<summary type="html">&lt;p&gt;Added short description&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;#039;&amp;#039;SearchForElement&amp;#039;&amp;#039; can be implemented generally using a specified type of iterator rather than implementing it as a container-specific algorithm. This allows &amp;#039;&amp;#039;SearchForElement&amp;#039;&amp;#039; 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(),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++11 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;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
#include &amp;lt;stdexcept&amp;gt;&lt;br /&gt;
#include &amp;lt;initializer_list&amp;gt;&lt;br /&gt;
		&lt;br /&gt;
class Vector {&lt;br /&gt;
public:&lt;br /&gt;
  using iterator = double*;&lt;br /&gt;
  iterator begin() { return elem; }&lt;br /&gt;
  iterator end() { return elem + sz; }&lt;br /&gt;
  &lt;br /&gt;
  Vector(std::initializer_list&amp;lt;double&amp;gt; lst) :elem(nullptr), sz(0) {&lt;br /&gt;
    sz = lst.size();&lt;br /&gt;
    elem = new double[sz];&lt;br /&gt;
    double* p = elem;&lt;br /&gt;
    for (auto i = lst.begin(); i != lst.end(); ++i, ++p) {&lt;br /&gt;
      *p = *i;&lt;br /&gt;
    }&lt;br /&gt;
  }  &lt;br /&gt;
  ~Vector() { delete[] elem; }&lt;br /&gt;
  int size() const { return sz; }&lt;br /&gt;
  double&amp;amp; operator[](int n) {&lt;br /&gt;
    if (n &amp;lt; 0 || n &amp;gt;= sz) throw std::out_of_range(&amp;quot;Vector::operator[]&amp;quot;);&lt;br /&gt;
    return elem[n];&lt;br /&gt;
  }&lt;br /&gt;
  Vector(const Vector&amp;amp;) = delete; // rule of three&lt;br /&gt;
  Vector&amp;amp; operator=(const Vector&amp;amp;) = delete;&lt;br /&gt;
private:&lt;br /&gt;
  double* elem;&lt;br /&gt;
  int sz;&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
int main() {&lt;br /&gt;
  Vector v = {1.1*1.1, 2.2*2.2};&lt;br /&gt;
  &lt;br /&gt;
  for (const auto&amp;amp; x : v) {&lt;br /&gt;
    std::cout &amp;lt;&amp;lt; x &amp;lt;&amp;lt; &amp;#039;\n&amp;#039;;&lt;br /&gt;
  }&lt;br /&gt;
  for (auto i = v.begin(); i != v.end(); ++i) {&lt;br /&gt;
    std::cout &amp;lt;&amp;lt; *i &amp;lt;&amp;lt; &amp;#039;\n&amp;#039;;&lt;br /&gt;
  }&lt;br /&gt;
  for (auto i = 0; i &amp;lt;= v.size(); ++i) {&lt;br /&gt;
    std::cout &amp;lt;&amp;lt; v[i] &amp;lt;&amp;lt; &amp;#039;\n&amp;#039;;&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;std::out_of_range&amp;#039;&lt;br /&gt;
  what():  Vector::operator[]&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>imported&gt;Tc14Hd</name></author>
	</entry>
</feed>