Design Pattern C ++ description ---- 20. Iterator Pattern

Source: Internet
Author: User

I. Examples

We know that Iterator is provided in STL to traverse the Vector or List data structure.

The Iterator mode is used to solve the traversal problem of an aggregate object. It encapsulates the traversal of the aggregate into a class, so as to avoid exposing the internal representation of the aggregate object.

For example, in STL, the following structures are available:

 


Ii. iterator Mode

Definition: provides a method to access each element in an aggregate object sequentially without exposing the internal representation of the object.

 

The typical example is the for_each operation in STL:

[Cpp] // function called for each element
Void print (int elem)
{
Cout <elem <'';
}

Int main ()
{
Vector <int> coll;

INSERT_ELEMENTS (coll, 1, 9 );

// For_each will call print (elem) for each elem)
For_each (coll. begin (), coll. end (), // range
Print); // operation
Cout <endl;
}

 

 

Author lwbeyond

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.