{
  "id": "enwiki-001320455755",
  "slug": "iterator-pattern",
  "title": "Iterator pattern",
  "qid": "",
  "lead": "In object-oriented programming, the iterator pattern is a design pattern in which an iterator is used to traverse a container and access the container's elements. The iterator pattern decouples algorithms from containers; in some cases, algorithms are necessarily container-specific and thus cannot be decoupled. For example, the hypothetical algorithm searchForElement() can be implemented generally using a specified type of iterator rather than implementing it as a container-specific algorithm. This allows searchForElement() to be used on any container that supports the required type of iterator.",
  "body": "In object-oriented programming, the iterator pattern is a design pattern in which an iterator is used to traverse a container and access the container's elements. The iterator pattern decouples algorithms from containers; in some cases, algorithms are necessarily container-specific and thus cannot be decoupled. For example, the hypothetical algorithm searchForElement() can be implemented generally using a specified type of iterator rather than implementing it as a container-specific algorithm. This allows searchForElement() to be used on any container that supports the required type of iterator. ==Overview== The Iterator design pattern is one of the 23 well-known \"Gang of Four\" design patterns 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. ===What problems can the Iterator design pattern solve?=== The elements of an aggregate object should be accessed and traversed without exposing its representation (data structures). New traversal operations should be defined for an aggregate object without changing its interface. 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 later without having to change the aggregate interface. ===What solution does the Iterator design pattern describe?=== Define a separate (iterator) object that encapsulates accessing and traversing an aggregate object. Clients use an iterator to access and traverse an aggregate without knowing its representation (data structures). Different iterators can be used to access and traverse an aggregate in different ways. New access and traversal operations can be defined independently by defining new iterators. See also the UML class and sequence diagram below. ==Definition== The essence of the Iterator Pattern is to \"Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation.\". import std; template using InitializerList = std::initializer_list ; using OutOfRangeException = std::out_of_range; template using UniquePtr = std::unique_ptr ; class DoubleVector { private: UniquePtr elements; size_t listSize; public: using Iterator = double*; nodiscard Iterator begin() const noexcept { return elements; } nodiscard Iterator end() const noexcept { return elements + listSize; } DoubleVector(InitializerList list): elements{std::make_unique (list.size())}, listSize{list.size()} { double* p = elements; for (auto i = list.begin(); i != list.end(); ++i, ++p) { *p = *i; } // alternatively implemented with // std::ranges::copy(list, elements.get()) } ~DoubleVector() = default; nodiscard size_t size() const noexcept { return listSize; } nodiscard double& operator[](size_t n) { if (n >= listSize) { throw OutOfRangeException(\"DoubleVector::operator[] out of range!\"); } return elements[n]; } DoubleVector(const DoubleVector&) = delete; // disable copy construction DoubleVector& operator=(const DoubleVector&) = delete; // disable copy assignment }; int main(int argc, char* argv[]) { DoubleVector v = {1.1 * 1.1, 2.2 * 2.2}; for (const double& x : v) { std::println(\"{}\", x); } for (size_t i = v.begin(); i != v.end(); ++i) { std::println(\"{}\", *i); } for (size_t i = 0; i The program output is 1.21 4.84 1.21 4.84 1.21 4.84 terminate called after throwing an instance of 'OutOfRangeException' what(): DoubleVector::operator[] out of range! ==References==",
  "categories": [
    "23118434",
    "23603337",
    "27395026",
    "27600972",
    "27695885",
    "36975041",
    "39691156",
    "57958211",
    "85249657"
  ],
  "license": "CC BY-SA 4.0",
  "source_uri": "https://en.wikipedia.org/w/index.php?oldid=1320455755",
  "content_hash": "",
  "created_at": null,
  "updated_at": 1762283182.0,
  "claims": [],
  "contradictions": [],
  "confidence": 0.6,
  "csl": {
    "id": "enwiki-001320455755",
    "type": "webpage",
    "title": "Iterator pattern",
    "author": [
      {
        "family": "Anticloud FZ LLE",
        "literal": "Anticloud FZ LLE"
      }
    ],
    "container-title": "encyclopegia.org",
    "publisher": "Anticloud FZ LLE",
    "publisher-place": "Dubai, United Arab Emirates",
    "URL": "https://encyclopegia.org/a/iterator-pattern",
    "issued": {
      "date-parts": [
        [
          2026,
          11,
          18
        ]
      ]
    },
    "accessed": {
      "date-parts": [
        [
          2026,
          11,
          18
        ]
      ]
    },
    "license": "CC BY-SA 4.0",
    "DOI": "eo1:enwiki-001320455755",
    "isBasedOn": "https://en.wikipedia.org/w/index.php?oldid=1320455755",
    "note": "Immutable snapshot enwiki-001320455755"
  }
}