Iterator in C ++

Source: Internet
Author: User

Containers in C ++

Standard sequence container: vector, String, deque, list

Standard associated containers: Set, Multiset, map, and multimap

 

Iterator Classification

Based on the operations supported by the iterator, The iterator can be divided into five categories.

1. Input iterator: it is a read-only iterator and can only be read once at each traversal location.

2. Output iterator: it is a write-only iterator that can only be written once at each traversal position.

3. Forward iterator: it provides both input and output iterators, but it can read and write repeatedly at the same position. But it does not support operator --, so it can only move forward.

4. Bidirectional iterator: similar to the forward iterator, it is easy to move backward and forward.

5. Random Access iterator: all functions of the two-way iterator are available. In addition, it provides an iterator for arithmetic, that is, any position that can jump forward or backward in one step.

 

The iterator provided in the C ++ container

1. The input and output iterator models are based on read/write operations on input and output streams (such as files. It is easy to understand that the most common manifestations of the input and output iterators are istream_iterator and ostream_iterator.

2. All standard STL containers support more powerful iterators than the forward iterator. (Except for one-way linked list containers of hash containers, which provide forward iterators)

3. Standard associated containers provide two-way iterators. This is also true for list.

4. vector, string, and deque all provide random access iterators. The internal pointer to the array is also a random access iterator for the array.

 

Istream_iterator and ostream_iterator usage example

Istream_iterator

1 // Istream_iterator example
2 # Include < Iostream >
3 # Include < Iterator >
4 Using   Namespace STD;
5
6 Int Main (){
7 Double Value1, value2;
8 Cout <   " Please, insert two values: " ;
9
10 Istream_iterator < Double > EOS; // End-of-stream iterator
11 Istream_iterator < Double > Iit (CIN ); // Stdin iterator
12
13 If (IIT ! = EOS) value1 = * Iit;
14
15 Iit ++ ;
16 If (IIT ! = EOS) value2 = * Iit;
17
18 Cout < Value1 <   " * "   < Value2 <   " = "   < (Value1 * Value2) < Endl;
19
20 Return   0 ;
21 }
22

 

Output:

Please, insert two values: 2 322*32 = 64
Ostream_iterator

1 // Ostream_iterator example
2 # Include < Iostream >
3 # Include < Iterator >
4 # Include < Vector >
5 Using   Namespace STD;
6
7 Int Main (){
8 Vector < Int > Myvector;
9 For ( Int I = 1 ; I < 10 ; ++ I) myvector. push_back (I * 10 );
10
11 Ostream_iterator < Int > Out_it (cout, " , " );
12 Copy (myvector. Begin (), myvector. End (), out_it );
13 Return   0 ;
14 }
15

 

Output:

10, 20, 30, 40, 50, 60, 70, 80, 90,

 

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.