C ++ Study Notes (3) iterator

Source: Internet
Author: User

Iterator
An iterator is used to access elements in a sequence. Its usage is similar to a pointer (in fact, an iterator can be seen as a generalized pointer ). The difference is that the iterator is more abstract than the pointer. It can point to a location in the container, and we don't have to worry about the real physical address corresponding to this location.

The iterator operation methods can be divided into the following five categories:


Input iterator

Read, but not write; increment only

Output iterator

Write, but not read; increment only

Forward iterator

Read and write; increment only

Bidirectional iterator

Read and write; increment and decrement

Random access iterator

Read and write; full iterator arithmetic


The following concept inheritance relationship diagram can clearly describe the relationships between these five classes. The more flexible the following iterator is, the more operation methods are.

 

For Input Iterator and output iterator, the common instances are stream iterators, which are istream_iterator and ostream_iterator. Their constructors are as follows:


Istream_iterator <T> in (strm );

Create istream_iterator that reads objects

Of type T from input stream strm.

Istream_iterator <T> in;

Off-the-end iterator for istream_iterator.

Ostream_iterator <T> in (strm );

Create ostream_iterator that writes objects

Of type T to the output stream strm.

Ostream_iterator <T> in (strm, delim );

Create ostream_iterator that writes objects

Of type T to the output stream strm using

Delim as a separator between elements.

Delim is a null-terminated character array.

For two input stream iterators, the same type must be used for Equality comparison. If they all point to the end of the stream, they are equal. If they do not point to the end of the stream, they are equal as long as they point to the same stream. This feature is often used to determine the end of the stream. The following is an example of using the input stream iterator:

[Cpp]
Istream_iterator <int> in_iter (cin); // read ints from cin
Istream_iterator <int> eof; // istream "end" iterator
Vector <int> vec (in_iter, eof); // construct vec from an iterator range

If the forward iterator does not have an input/output iterator, the auto-increment operation and write (or read) operations must be carried out alternately. It can perform read/write operations freely, but only auto-increment operations are allowed, it cannot be auto-subtracted.
The two-way iterator is more flexible than the forward iterator. It adds auto-subtraction operations on the basis of the two-way iterator. Provides two-way iterators to map, set, and list containers.

The Random Access iterator can be seen from the name. It supports Random Access to containers and other operations. Based on the bidirectional iterator, it also supports p + = n, p-= n, p + n, p-n, p1-p2, p1 or p2. Containers such as string, vector, and deque provide random access iterators. We can see that these containers adopt continuous data structures.

The above division of the iterator is based on their concept classification. In fact, we can also divide it from the perspective of its use, such as the reverse iterator. Using container. rbegin (), container. the two container functions rend () can get the reverse iterator of the container. The operation method is the opposite to that of the normal iterator. We can use them to perform reverse iteration on the container.

When using the reverse iterator, you must pay attention to the elements that it actually points to. See the following example. Assume that there is such a string line:

FIRST, MIDDLE, LAST

To print the last word, we may write C ++ Code as follows:

[Cpp]
String: reverse_iterator rcomma = find (line. rbegin (), line. rend (),',');
Cout <string (line. rbegin (), rcomma) <endl;

However, the printed result is: TSAL
If you think about it, this result is normal, because the reverse iterator is originally a reverse iteration of the container. But this is not what we want. To get the print result of last, we need to modify the Code as follows:

[Cpp]
Cout <string (rcomma. base (), line. end () <endl;

The base function of the reverse iterator can be called to obtain the corresponding positive iterator. Some children's shoes may be confused about its iteration range because it does not print a comma, in fact, rcoma and rcoma. base () does not point to the same element. The reason for the design in STL is that the Left-closed and right-open principle of the sequence makes rcomma. base (), line. end () and line. rbegin () and rcomma point to the same sequence. The following figure shows more clearly:

 


Author: justaipanda

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.