C + + NOTE------iterators

Source: Internet
Author: User

STL is a generic programming . Object programming focuses on the data aspects of programming , and generic programming focuses on the common use of algorithms, the abstraction of commonalities between them, and the creation of reusable code, but with different philosophies.

STL uses the term " concept " to describe a series of requirements that an iterator needs to meet, such as a forward iterator as a requirement, not a type.

STL uses the term " improved " to represent this conceptual inheritance, which has a similar inheritance relationship, but cannot use C + + inheritance mechanisms for iterators.

The implementation of a concept is called a model , so a regular pointer to int is a model of a random-access iterator and a model of a forward iterator.

The STL first defines the appropriate iterator type for each container class, which can be a pointer or an object. Iterators will provide some basic operations, such as * and + +.

Next, each container class has an extra-tailed tag that iterates through each element of the container by letting the iterator + + operate from the first element to the over-tailed position.

The STL defines 5 types of iterators, input iterators , output iterators , forward iterators , bidirectional iterators , and random-access iterators . These 5 kinds of iterators can perform de-application operations, and if two iterators are the same, the values that are obtained from dereferencing them are the same. The characteristics of the iterators and the characteristics of the containers are designed based on the algorithm requirements.

1. Input iterators:

(1) "Input" means getting information from a container, so the input iterator is used to read the information in the container, but the program cannot modify the value.

(2) The input iterator is unidirectional and can be incremented using the + + operator, but cannot be reversed.

2. Output iterator:

(1) "Output" means that information is transferred from the program to the container, so that the program can only modify the container's value, but not read it.

(2) The output iterator is also unidirectional and can be incremented using the + + operator, but cannot be reversed.

Ps: For single-pass, write-only algorithms, output iterators can be used, and for single-pass, read-only algorithms, an input iterator can be used.

3. Forward iterators:

(1) Forward iterators use the + + operator only to traverse the container, which can be traversed in the same order.

(2) After incrementing the forward iterator, you can still dereference the previous iterator (the saved) value, and you can get the same value.

(3) Forward iterators can read and modify data as well as read-only data.

4. Bidirectional iterators:

(1) contains the attributes of all forward iterators, and supports the decrement operator (a--and--a).

5. Random iterators:

(1) The ability to jump directly to any element in the container for random access, including the characteristics of all bidirectional iterators.

Ps:

1. When writing an algorithm, you can increase the versatility of the algorithm by using the lowest-level iterator. For example, the fing () function can be used with any container that contains a readable iterator. The sort () function requires a random access iterator, so only the container that supports this iterator.

2. The type of the various iterators is not deterministic, but a conceptual description. Each container class defines a typedef name at a class level-----iterator.

As previously mentioned, iterators are generalized pointers, and pointers meet all the requirements of iterators, so the STL algorithm can operate on pointers-based non-STL containers.

For example:

C + + uses the Hyper-tailed concept for arrays, and STL algorithms can be used for regular arrays. Pointers are also iterators and make the STL algorithm available to regular arrays.

#include <iostream>#include<iterator>#include<algorithm>#include<vector>using namespacestd;voidOutputConst ints) {cout << s <<" "; }intMain () {intcasts[Ten] = {6,7,2,9,4, One,8,7,Ten,5 }; Std::vector<int> Dice (Ten); //copying data from one container to another//The first two parameters replicate the range, and the last parameter is copied to where. //the target container must be large enough to accommodate the elementCopy (casts, casts +Ten, Dice.begin ());  for(Auto x:casts) cout << x <<" ";//Range-based for loopcout <<Endl; Ostream_iterator<int,Char>out_iter (cout," "); //adapter--a class or function that transforms other interfaces into interfaces used by STL//first parameter: refers to the data type to be sent to the output stream//Second parameter: Indicates the type of character used by the output stream (possibly wchar_t)//first parameter of constructor: indicates the output stream used//second parameter: The delimiter displayed after each data item of the output streamcopy (Dice.begin (), Dice.end (), out_iter); cout<<Endl; *out_iter++ = the;//send a string consisting of 15 and spaces to the cout managed output stream for cout << << "";cout <<Endl; //can be used directly without creating a named iteratorCopy (Dice.begin (), Dice.end (), ostream_iterator<int,Char> (cout," ")); Std::vector<int> Dice1 (Ten); Vector<int>:: Iterator RP;  for(RP = Dice1.begin (); RP! = Dice1.end (); rp++) Cin>> *RP;    For_each (Dice1.begin (), dice1.end (), output); cout<<Endl; Vector<int>::reverse_iterator RI;//performing an incremental operation on a revere_iterator causes it to decrement itself//Rbegin () and end () return values, but not the same type (Reverse_iterator and iterator)//rend () and begin () return the same type, return the address that points to the first element//before the RI is de-referenced, it is self-decrement before dereferencing. Example: Ri execution position 6,*ri is a numeric value of position 5     for(ri = Dice1.rbegin (); RI! = Dice1.rend (); + +ri) cout<< *ri <<" "; cout<<Endl; return 0;}

Ps:

1. Why does RI need to be self-reducing first?

Because Rbegin () returns an extra-tail, it is not possible to dereference, so rend () returns the first element address, so one position must be stopped early (the end of the interval is not included in the interval).

In addition to the above used iterators, there are three types of insert iterators:

Back_insert_iterator: Inserting an element into the container trailer

Front_insert_iterator: Inserting elements into the container front end

Insert_iterator: Inserts an element in front of the specified position

The above three kinds of iterators can improve the generality of STL algorithm, and can use the container type as the template parameter. For example:

back_insert_iterator<vector<int> > Back_iter (DICE)

insert_iterator<vector<int> > Insert_iter (Dice,dice.begin ());

#include <iostream>#include<vector>#include<iterator>#include<algorithm>#include<string>voidOutputConstSTD::string&s) {Std::cout << s <<" "; }using namespacestd;intMain () {strings1[4] = {"Fine","Fish","Fashion","Fate" }; strings2[3] = {"I","am","Zhang" }; strings3[2] = {"Zeze","Good" }; Vector<string> Words (4); Copy (S1,s1+4, Words.begin ());    For_each (Words.begin (), words.end (), output); cout<<Endl; Copy (S2,s2+2,front_insert_iterator<vector<string> >(words));    For_each (Words.begin (), words.end (), output); cout<<Endl; Copy (S3,s3+2,insert_iterator<vector<string> >(Words,words.begin ()));    For_each (Words.begin (), words.end (), output); cout<<Endl; return 0;}

C + + NOTE------iterators

Related Article

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.