iterators: In addition to accessing container elements in a way that is commonplace in other languages, the C + + language provides an entirely new approach-iterators (iterator) to access the elements of a container. An iterator is actually similar to a reference, pointing to an element in the container. In other words, the container is the data structure of the generic, the iterator is the pointer to the generic, can point to the element. A container is the equivalent of a storage cabinet in which many different items are stored as elements such as bread, beer, apples, and cash. To obtain each object, a tool must be used to match the object, such as removing the bread to use a plate, taking out the beer to use the cup, removing the apples to use the basket, and taking out the cash to use the purse. An iterator acts as an abstraction of the tool that takes out an item, and an iterator refers to a real-life tool that extracts objects from a storage room. A C + + iterator is a data type that examines the elements inside a container and iterates through the elements.
Types of iterators
For STL data structures and algorithms, you can use five types of iterators. The following is a brief description of these five types:
Input Iterators provides read-only access to the data.
Output Iterators provides write-only access to data.
The Forward iterators provides read and write operations and can forward iterators.
The bidirectional iterators provides read and write operations and can be operated forward and backward.
The random access iterators provides read and write operations and can be moved randomly in the data.
Although the different STL implementation details differ, the above iterators can be imagined as a class inheritance relationship. In this sense, the following iterator inherits from the iterator above. Because of this inheritance, you can use a forward iterator as an output or input iterator. Similarly, if an algorithm requires a bidirectional iterator, then only that type and random access iterator can be used.
Pointer iterator
A pointer is also an iterator. The program also shows a major feature of the STL-it is not only available for its own class type, but also for any C or C + + type. The following program shows how to use pointers as iterators for the STL's find () algorithm to search for normal arrays.
1#include <iostream.h>2#include <algorithm>3 4 using namespacestd;5 #defineSIZE 1006 intIarray[size];7 8 intMain ()9 {Teniarray[ -] = -; One int* ip = Find (iarray, IArray + SIZE, -); A if(IP = = IArray +SIZE) -cout <<"found in array"<<Endl; - Else thecout << *ip <<"found in array"<<Endl; - return 0; -}
A global array of size is defined in the program. Because it is a global variable, the run-time array is automatically initialized to zero. The following statement sets the place element at index 20 to 50 and uses the Find () algorithm to search for the value 50:
IARRAY[20] = 50;
int* IP = Find (iarray, IArray + SIZE, 50);
The Find () function accepts three parameters. The first two define the scope of the search. Because the C and C + + arrays are equivalent to pointers, the expression IArray points to the first element of the array. The second parameter, IArray + size, is equivalent to the Past-the-end value, which is the trailing position of the last element in the array. The third parameter is the value to be positioned, which is 50. The Find () function returns an iterator of the same type as the first two arguments, and here is a pointer to an integer IP.
if (IP = = iarray + SIZE) ...
If the expression is true, it indicates that there are no values specified in the scope of the search. Otherwise, a pointer to a legitimate object can be displayed with the following statement:
cout << *ip << "found in array" << Endl;
It is incorrect to test whether the function return value and null are equal. Do not use it as follows:
int* IP = Find (iarray, IArray + SIZE, 50);
if (IP! = NULL) ...//??? Incorrect
When using STL functions, you can only test if the IP and past-the-end values are equal. Although in this case IP is a C + + pointer, its usage must conform to the rules of the STL iterator.
Container iterators
Although the C + + pointer is also an iterator, it uses more of a container iterator. Unlike declaring iterators as pointer variables, you can use the container class method to get an iterator object. The two typical container class methods are begin () and end (). They represent the entire range of containers in most containers. Other containers also use the Rbegin () and Rend () methods to provide reverse iterators to specify the range of objects in reverse order.
The following program creates a vector container (STL and array-equivalent objects) and uses iterators to search for them. The program is the same as the procedure in the previous chapter.
1#include <iostream.h>2#include <algorithm>3#include <vector>4 5 using namespacestd;6vector<int> Intvector ( -);7 8 voidMain ()9 {Tenintvector[ -] = -; Onevector<int>::iterator Intiter = AFind (Intvector.begin (), Intvector.end (), -); - if(Intiter! =intvector.end ()) -cout <<"Vector contains value"<< *intiter <<Endl; the Else -cout <<"Vector does not contain"<<Endl; -}
Const iterator
As with pointers, you can assign values to an iterator. For example, first declare an iterator:
Vector<int>::iterator first;
The statement creates an iterator to the Vector<int> class. The following statement sets the iterator to the first object of Intvector and sets the object value it points to 123:
First = Intvector.begin ();
*first = 123;
This assignment is allowed for most container classes, except for read-only variables. To prevent error assignment, you can declare that the iterator is:
Const Vector<int>::iterator Result;
result = Find (Intvector.begin (), Intvector.end (), value);
if (Result! = Intvector.end ())
*result = 123; // ???
Warning: Another way to prevent data from being changed is to declare the container as a const type.
Concrete iterator implementation of the source code,https://www.sgi.com/tech/stl/download.html on it, you can come down to see.
The six components of STL--iterators this thing