An iterator is an object that can be used to traverse some or all elements in the STL container. Each iterator object represents a specific address in the container. The iterator modifies the interface of the regular pointer. The so-called iterator is a conceptual Abstraction: What acts like the iterator can be called the iterator. However, the iterator has many different capabilities, which can organically unify abstract containers and general algorithms.
The iterator provides some basic operators: *, ++, =, and ,! =, =. These operations are the same as the pointer interface used to operate the array element in C/C ++. The difference is that the iterator is a so-called smart pointers and has the ability to traverse complex data structures. Its underlying operating mechanism depends on the data structure it traverses. Therefore, each container type must provide its own iterator. In fact, each container defines its iterator in a nested manner. Therefore, the interfaces of various iterators are the same, but the types are different. This directly derives the concept of generic programming: All operation behaviors use the same interface, although their types are different.
Function
The iterator enables developers to support foreach iteration in the class or structure, without the need to implement the entire ienumerable or ienumerator interface. You only need to provide an iterator to traverse the data structure in the class. When the compiler detects the iterator, the current, movenext, and dispose methods of the ienumerable interface or the ienumerator interface are automatically generated.
Features
1. The iterator is a piece of code that can return an ordered sequence of the same type of values;
2. The iterator can be used as a method, operator, or get accessor code body;
3. The iterator Code uses the yield return statement to return each element in sequence, and the yield break will terminate the iteration;
4. Multiple iterators can be implemented in the class. Each iterator must have a unique name like any class member and can be called by the client code in the foreach statement;
5. The return type of the iterator must be ienumerable or ienumerator;
6. The iterator is a statement block that generates an ordered sequence of values. It is different from the regular statement block that exists in one or more yield statements;
7. an iterator is not a member. It is only a member of a function. It is important to understand this. It is a member implemented by an iterator, it can be overwritten and overloaded by other members that may or cannot be implemented through the iterator;
8. iterator blocks are not unique elements in the C # syntax. They are restricted in several aspects and mainly apply to the semantics of function member declarations. They are only statement blocks in syntax;
A simple example
# Include <iostream>
Using namespace STD;
Class _ iterator
{
PRIVATE:
Char * P;
Public:
_ Iterator (char * Str): p (STR ){}
Char * & operator ++ ()
{
P + = 1; // same price as P ++;
Return P;
}
};
Int main ()
{
Char * P = "This is C ++ program ";
_ Iterator it (P );
Cout <"++ before:" <p <Endl;
Char * P1 = ++ it; // Add a char length to the forward address, and then give the pointer p1
Cout <"+ +:" <P1 <Endl;
Return 0;
}
Example of traversing a vector container
// Equivalent loop using iterators to reset all the elements in ivec to 0
# Include <iostream>
# Include <vector>
Using namespace STD;
Int main ()
{
Vector <int> ivec (10, 42 );
For (vector <int >:: iterator iter = ivec. Begin (); iter! = Ivec. End (); ++ ITER)
* Iter = 0;
Return 0;
}