Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the iterator pattern of C++ design pattern

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

In this article, the editor introduces in detail "how to realize the iterator pattern of C++ design pattern". The content is detailed, the steps are clear, and the details are handled properly. I hope that this article "how to implement the iterator pattern of C++ design pattern" can help you solve your doubts.

Iterator is widely used in STL, similar to container iteration has become an important feature, while iterator pattern is an abstract use of the concept of iterator, iterator pattern is widely used and useful, because it can not consider the way the data is stored, but directly face the data iteration, that is, we do not have to consider whether the set is an array (or vector), linked list, stack or queue It is accessed sequentially through a unified interface.

Action

The iterator pattern provides a way to access the elements in the container sequentially without knowing the internal type and structure of the container. The core idea of this pattern is to give the function of accessing and traversing the container object to an external iterator object. The iterator defines the interface to access the aggregate object.

Class view

Realize

Class Item {public: Item (string strName,float price): m_name (strName), m_price (price) {} Item (Item& item): m_name (item.strName), m_price (item.price) {} string tostring () {std::ostringstream buffer; buffer = 0 & & curpos

< m_menu->

Size ()} virtual Item* current () {return masked menu-> value (curpos);}}; class Menu: public Container {public: virtual ~ Menu () {for (int item0; I)

< m_items.size(); i++) { delete m_items[i]; } }; Iterator* CreateIterator() { return new MenuIterator(this); } int size() { return m_items.size(); } Item* value(int nIndex) { if(nIndex>

= 0 & & nIndexhasnext () {Item* pItem = itor- > current (); if (pItem) cout

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report