05th ACM/ICPC competition STL-iterator Introduction

Source: Internet
Author: User

Iterator (iterator) is an indicator used to access elements in the container. In this sense, iterator (iterator) is equivalent to "traversal pointer" in the data structure ", iterator can also be considered as a generalized pointer.

The implementation of iterator (iterator) in STL is quite complicated. Here we will not discuss in detail the implementation and use of iterator (iterator) for the moment, but only for iterator (iterator) let's give a brief introduction.

In short, STL has the following types of iterator ):

Input iterator (iterator) to move forward within the continuous interval of the container, which can read any value in the container;
Output iterator and write the value into the container to which it points;
Forward iterator (iterator) reads the values in the queue and can move forward to the next position (++ p, p ++ );
Two-way iterator (iterator), reads the values in the queue, and can traverse the container forward and backward;
Random Access to the iterator (iterator), which can be directly accessed to the container in the following way. The iterator (iterator) of the vector is the iterator (iterator );
Stream iterator (iterator), which can directly output and input values in the stream;
Each STL container has its own iterator (iterator) subclass. Let's take a look at a simple sample code:

# Include <iostream>
# Include <vector>
Using namespace STD;
Main ()
{
Vector <int> S;
For (INT I = 0; I <10; I ++) S. push_back (I );
For (vector <int >:: iterator it = S. Begin (); it! = S. End (); It ++)
Cout <* It <"";
Cout <Endl;
Return 1;
}

The begin () and end () Methods of vector return a vector: iterator object, point to the position of the first element and the next position of the tail element of the vector respectively (we can call it the position of the ending sign ).

The use of an iterator object is very similar to the use of a pointer variable. Alternatively, a pointer is a very standard iterator (iterator ).

Let's take a look at a bit of code:

# Include <iostream>
# Include <vector>
Using namespace STD;
Main ()
{
Vector <int> S;
S. push_back (1 );
S. push_back (2 );
S. push_back (3 );
Copy (S. Begin (), S. End (), ostream_iterator <int> (cout ,""));
Cout <Endl;
Return 1;
}

In this Code, copy is a template function defined in STL. Copy (S. begin (), S. end (), ostream_iterator <int> (cout. begin () to S. end () (excluding S. the sequence specified by end () is copied to the cout of the standard output stream, and "" is used as the interval of each element. That is to say, the role of this statement is to output all the content in the Table in sequence.

Iterator (iterator) is the "binder" between STL containers and algorithms. Almost all STL algorithms access container content through container iterator (iterator. Only by effectively using iterator can we effectively use STL's powerful Algorithm functions.

 

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/camel_flying/archive/2009/08/17/4454182.aspx

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.