--- url: "https://refactoring.guru/design-patterns/iterator/cpp/example" titulek: "Iterator in C++ / Design Patterns" h1: "Iterator in C++" popis: "Iterator pattern in C++. Full code example in C++ with detailed comments and explanation. Iterator is a behavioral design pattern that allows sequential traversal through a complex data structure without exposing its internal details." jazyk: "en" kanonicka: "https://refactoring.guru/design-patterns/iterator/cpp/example" drobecky: ["Home", "Design Patterns", "Iterator", "C++"] zdroj: "live" wayback_snapshot: "" stazeno: "2026-09-13 21:29:14" znaku: 3594 slov: 499 kodovani: "utf-8" sha256: "f1f12ff467f88d0dfb600985384702769e7e4090de6fb8e734ce061c41f04b04" --- # **Iterator** in C++ **Iterator** is a behavioral design pattern that allows sequential traversal through a complex data structure without exposing its internal details. Thanks to the Iterator, clients can go over elements of different collections in a similar fashion using a single iterator interface. [Learn more about Iterator](/design-patterns/iterator) **Complexity:** **Popularity:** **Usage examples:** The pattern is very common in C++ code. Many frameworks and libraries use it to provide a standard way for traversing their collections. **Identification:** Iterator is easy to recognize by the navigation methods (such as `next`, `previous` and others). Client code that uses iterators might not have direct access to the collection being traversed. ## Conceptual Example This example illustrates the structure of the **Iterator** design pattern. It focuses on answering these questions: - What classes does it consist of? - What roles do these classes play? - In what way the elements of the pattern are related? #### **main.cc:** Conceptual example ``` /** * Iterator Design Pattern * * Intent: Lets you traverse elements of a collection without exposing its * underlying representation (list, stack, tree, etc.). */ #include #include #include /** * C++ has its own implementation of iterator that works with a different * generics containers defined by the standard library. */ template class Iterator { public: typedef typename std::vector::iterator iter_type; Iterator(U *p_data, bool reverse = false) : m_p_data_(p_data) { m_it_ = m_p_data_->m_data_.begin(); } void First() { m_it_ = m_p_data_->m_data_.begin(); } void Next() { m_it_++; } bool IsDone() { return (m_it_ == m_p_data_->m_data_.end()); } iter_type Current() { return m_it_; } private: U *m_p_data_; iter_type m_it_; }; /** * Generic Collections/Containers provides one or several methods for retrieving * fresh iterator instances, compatible with the collection class. */ template class Container { friend class Iterator; public: void Add(T a) { m_data_.push_back(a); } Iterator *CreateIterator() { return new Iterator(this); } private: std::vector m_data_; }; class Data { public: Data(int a = 0) : m_data_(a) {} void set_data(int a) { m_data_ = a; } int data() { return m_data_; } private: int m_data_; }; /** * The client code may or may not know about the Concrete Iterator or Collection * classes, for this implementation the container is generic so you can used * with an int or with a custom class. */ void ClientCode() { std::cout << "________________Iterator with int______________________________________" << std::endl; Container cont; for (int i = 0; i < 10; i++) { cont.Add(i); } Iterator> *it = cont.CreateIterator(); for (it->First(); !it->IsDone(); it->Next()) { std::cout << *it->Current() << std::endl; } Container cont2; Data a(100), b(1000), c(10000); cont2.Add(a); cont2.Add(b); cont2.Add(c); std::cout << "________________Iterator with custom Class______________________________" << std::endl; Iterator> *it2 = cont2.CreateIterator(); for (it2->First(); !it2->IsDone(); it2->Next()) { std::cout << it2->Current()->data() << std::endl; } delete it; delete it2; } int main() { ClientCode(); return 0; } ``` #### **Output.txt:** Execution result ``` ________________Iterator with int______________________________________ 0 1 2 3 4 5 6 7 8 9 ________________Iterator with custom Class______________________________ 100 1000 10000 ``` ## **Iterator** in Other Languages