Analysis of STL source code--classification of each container iterator

Source: Internet
Author: User

The five types of iterators are as follows:

1. Input iterator: read-only, one pass
The predefined implementations for input iterators are only Istream_iterator and istreambuf_iterator, which are used to read from an input stream istream. An input iterator can only parse each element of its choice once, and they can only move forward. A specialized constructor defines the value beyond the end. Always, an input iterator can parse the results of a read operation (once for each value) and move forward.
2, Output iterator: Write only, one pass
This complements the input iterator, but is a write operation rather than a read operation. The predefined implementations for output iterators are only Ostream_iterator and ostreambuf_iterator, which are used to ostream write data to an output stream, and there is a generally less-used raw_storage_iterator. They can only parse once for each value written, and move forward only. For an output iterator, the concept of ending with a value beyond the end is not used. In summary, an output iterator can parse the value of a write operation (only once for each value), and then move forward.
3. Forward iterators: Read/write multiple times
The forward iterator contains the functions of both the input and output iterators, plus the position specified by an iterator can be parsed multiple times, so that a value can be read/written multiple times. As the name implies, forward iterators can only move forwards. There are no predefined iterators for the forward iterator.
4, bidirectional iterator: operator--
Bidirectional iterators have all the functions of a forward iterator. It can also use the operator--operator to move backward one position at a time. The iterator returned by the list container is bidirectional.
5. Random-access iterators: Similar to a pointer
A random-access iterator has all the functions of a bidirectional iterator, plus a pointer-all function (a pointer is a random-access iterator), except that there is no "empty (null)" iterator and a null pointer corresponding to it. Basically, a random-access iterator can do anything like a pointer, including using the operator operator[] to index, add a value to a pointer to move forward or backward, or use comparison operators to compare between iterators.

The vectors and deque provided by Randomaccessiterator,list are provided by Bidirectionaliterator,set and map provide iterators is bidirectional Iterator, Hash_set and Hash_map iterators are Forward Iterator. The operations for iterator iterators in STL are as follows:

Iterator category

Description

Input iterator

Reads the element from the container. Input iterators can only read one element at a time and move forward, the input iterator supports only one pass, and the same input iterator cannot traverse a sequence two times

Output iterator

Forward iterator

Combines the functionality of the input iterator and output iterator and retains its position in the container

Bidirectional iterator

Functions of combining forward iterators and reverse iterators, supporting multiple passes

Random-access iterator

Combined bidirectional iterator functionality with direct access to any element in the container, You can skip forward and backward through any element

Operation of the iterator:

Each iterator can perform operations that include the previous iterator in the table. The operation of an iterator is essentially implemented by overloading the operator, and what the iterator supports and what it can do is determined by the operator overloaded by the iterator.

Iterator type Type of operation Description

All iterators

p++

++p

Post-built self-increment iterators

Predecessor self-increment iterator s ' s

Input iterators

*p

P=p1

P==p1

P!=p1

Complex reference iterators, as Rvalue

Assigning an iterator to another iterator

Compare the equality of iterators

Comparing the inequality of iterators

Output iterators

*p

P=p1

Complex reference iterators, as Lvalue

Assigning an iterator to another iterator

Forward iterators

Provides all the functions of an input-output iterator

Bidirectional iterators

--p

p--

Pre-built-in self-reducing iterators

Post-offset self-reducing iterators

Random-Access iterators

P+=i

P-=i

P+i

P-i

P[i]

P<p1

P<=p1

P>p1

P>=p1

Increments the iterator I bit

Decrements the iterator I bit

Iterators after P-bit plus I-bit

The iterator after the P-bit minus I bit

Returns the element reference of the P-bit element deviating from the I bit

Returns true if the position of the iterator P is before P1, otherwise false

The position of P returns true at the front of the P1 or in the same position, otherwise it returns false

Returns true if the position of the iterator P is P1, otherwise false

The position of P returns true at the back of the P1 or in the same position, otherwise it returns false

Only sequential containers and associative containers support iterator traversal, and the categories of iterators supported by each container are as follows:
Iterator categories supported by container-supported iterator category containers for containers
Vector random Access deque random access list bidirectional
Set bidirectional multiset bidirectional map double To
Multimap bidirectional stack does not support queue not supported
Priority_queue does not support hash_set forward hash_map forward

Analysis of STL source code--classification of each container iterator

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.