Several usage of the iterator mode when reading the post on the network, I found that many beginners of the mode understand the iterator mode only by obtaining the iterator from the container class of the class library to traverse the content in the container. So here are a few examples to help you better understand the iterator model. Access to elements in the container involves three aspects. 1. Container Type 2. Method of retrieving elements in the container 3. Operations on elements for example, we have a book_store class that represents the bookstore. It stores various book-class instances. The book class has two attributes: name and type. The name and category of a book. Therefore, the book_store class uses a container to store the book instance. For example, list. Class book {public: string name; string type ;}; class book_store {private: List <book> m_books ;}; now we have a simple application, that is, output all titles to the screen. Let's take a look at the several implementation methods before the introduction of the iterator mode, and what are their respective shortcomings. 1. Add a print_book_name function to the book_store class. To implement this function. There are two disadvantages: (1). Bind the input/output logic with the business object, making it difficult to change the system in the future. (2). Bind the output logic to the internal implementation. For example, if you want to change the output to the screen to the log file, you need to modify print_book_name. 2. Add a get_list function to the book_store class. Then write a print_book class to print the list of titles. This is a common method for beginners. On the surface, it seems that two classes are responsible for each other. One represents the business object and the other is responsible for printing. If you need to print to the log file, as long as a new log_book class is added, book_store and print_book are not affected. Solved the problem above. The disadvantage of doing so is: (1) exposing the internal implementation of the book_store class, in violation of the encapsulation principle. If we change the internal container type from list to vector, we need to modify the output logic. That is to say, the print_book class and the log_book class must be rewritten at the same time. 3. Add a base class/interface that is common to the list and vector classes, such as I _container. Then add a get_books function to the book_store class and return I _container. This solves the above problems to a certain extent. But not completely. The internal implementation should be exposed, but the list is increased to I _container. · If the current system changes, book_store no longer saves books locally, but needs to be obtained through the network, print_book and log_book cannot match. · Another limitation is that if I need a new function, that is, to print all the functions of the type as "Success book", I need a print_cook_book class, the formatting code and output code here are the same as those of print_book. Repetitive code is a nightmare of maintenance. Next, let's take a look at how the iterator mode solves these problems. First, we introduce an I _iterator interface. Create an I _iterator implementation class all_book_iterator. This class can iterate all the books in book_store. Next, create a print_book class, obtain each book from I _iterator, and print it to the screen. Next, let's take a look at how to adapt to the above demand changes. 1. You need to append a log_book class to the log file for output. Other classes do not need to be changed. 2. Change the internal container from list to vector. Modify the code in iterator. Users of iterator will not be affected. 3. obtain data from the network. Modify the code in iterator. Users of iterator will not be affected. 4. Add a type attribute to iterator by type search, or construct a new type_search_iterator. Filter out books that do not meet the search criteria in iterator. 5. output the book name to the screen or log file in a specific order. Implement an iterator output in this order. 6. Print cd_store in the same way. Implement iterator corresponding to cd_store. It can be seen that the iterator mode encapsulates the retrieval element methods in various iterator. Completely separate the container from the operations on elements in the container. This greatly increases code reusability.