Iteration is a mathematical concept.
Let me talk about it.
A man and a woman multiply and have a son.
This son, along with a woman, has created a child that was the son of the old man.
This son, along with a woman, has created a child that was the great son of the man before (the first line.
Reproduction is the most common iteration process in nature.
Repeating the behavior of "breeding future generations" means repeating the "iteration" process.
An iterator is a program encapsulated to repeat the "iteration" process.
Each language can implement the iterator.
Knowledge about the iterator in C ++ STL can be learned by combining the list template class.
List <string> myfriends;
This defines a list template class instance named myfriends.
The relationship between classes and instances, you know?
In philosophy, classes are a collection of abstract concepts, and "instances" are the embodiment of abstract concepts.
Okay, we have a list class instance of myfriends,
Now, we want to put things in your friend list.
How to put it?
Now that you have an instance of a class, you need to operate it in it, for example, to put or discard something, you should call the function.
This type of function is also called method ).
For the myfriends instance, you have to check the template class description.
Well, you will know:
The list template class has two methods:
Push_back and push_front.
As the name implies
Push_back: put new things behind the current object;
Push_front: place new things in front of the current object.
The "Current object" here refers to the List class instance.
Now, start adding friends.
Myfriends. push_back ("Insects ");
Myfriends. push_back ("Soy Sauce Emperor ");
Myfriends. push_front ("Zhang Wuji ");
Myfriends. push_front (" ");
What is the order? Don't worry. We can verify it at last.
All of my friends put it in. Now let's name it!
At this time, we need to invent a "vertex", right?
The principle of the "vertex handler" is:
The name of the person you want to name.
Is this process a repetitive process, right?
Therefore, the "vertex handler" is an "iterator ".
Okay, the iterator is on the stage.
C ++ STL has already prepared the template class "iterator" for you.
Let's wait for you to take the abstract concept of this template class to generate an instance of "vertex name server,
How to generate?
Use this statement:
List <string >:: iterator myfriendscallhisname;
"Iterator" is the iterator.
Myfriendscallhisname is the name of the iterator.
You can also write
List <string >:: iterator baomingzi;
The name "myfriendscallhisname" is to highlight its relationship with the list object "myfriends.
Now, start to name.
As I said just now, the naming process is a repetitive process. We use the most basic Repeating Function in C. What is it?
What is it?
-- A:
Yes!
Come on.
For a play.
For (initial value; condition; what to continue)
{
Name
}
For (...;...) {the process in this will call some functions}
But don't worry,
For (initial value; condition; what to continue)
This issue has not been solved yet.
What is the initial value?
What are the conditions?
What are you doing?
-- Answer: For (INT I = 1; I <= 100; I ++)
This is the old practice without an iterator,
There are many problems with such "soil practices:
How do you know the condition is "<= 100", where did 100 come from?
To be accurate, with the iterator, you should not care about it at all. What is the value in this condition.
-- Answer: there are only four names. Just repeat it four times.
The problem is that the list will change at any time. You cannot count it by yourself.
Then, the advantages of the iterator are revealed.
Okay, let's try it one by one.
Initial Value
Note: "For (INT I = 1; I <= 100; I ++ )"
The "int I" in it actually has nothing to do with the List itself. It is an additional number we designed, right?
-- A: Well, I can't wait to know how to use it?
You can't rush. I want to explain the reason for using the iterator. Otherwise, I will not use it to get off my pants and fart.
Please understand this sentence:
The "int I" in it actually has nothing to do with the List itself. It is an additional number we designed, right?
The key is that this number has nothing to do with the list of friends.
The idea of the iterator is: for the friend list itself!
What is a friend list? It's "myfriends.
This is an instance of the list template class.
To operate an instance, you must use the method of the class that the instance belongs to, also known as "member function ".
Next, there is a new "member function"-you have to look up the class instructions.
Myfriends. Begin ()
In this "begin ()" method, The first thing in myfriends is "handed in ".
To whom?
To the iterator.
That is:
Myfriendscallhisname = myfriends. Begin ();
This statement reflects the relationship between a list object and its iterator.
More importantly, this statement will become the initial value of ".
That is to say, the initial value of "for" is not an extra number, but an iterator closely linked to the object we want to operate.
This is only the initial value,
Condition,
Since the initial value is an iterator, the condition should also be related to the iterator.
We will sign up from start to end,
The initial value is the first friend.
Then, the end should be the last friend.
Therefore, set the condition to "myfriendscallhisname! = Myfriends. End ();"
-- Question: Wait ...... Myfriendscallhisname! = Myfriends. End (); is not equal to the last member. Isn't it possible? The initial value is the first, and the condition is not equal to the last one.
Don't worry. You can use the pack!
Why?
The key is the last "what to continue ".
This involves a programming technique.
We usually like "I ++" and "++ I" in C.
Therefore, the "I ++" in your original "",
It should be replaced with the iterator:
++ Myfriendscallhisname
Okay. (.;.;.) Well done:
For (
Myfriendscallhisname = myfriends. Begin ();
Myfriendscallhisname! = Myfriends. End ();
++ Myfriendscallhisname
)
In this case, you must keep in mind that it always has something to do with the iterator!
We may use "stream" to output the name.
That is, cout <...... <Endl;
The key is the intermediate "..." What is the content?
Remember: it always has something to do with the iterator!
This uses the old focus of C/C ++-pointer or reference.
Through our example, we vaguely felt that the value of myfriendscallhisname
It seems to be related to the pointer or reference of each object in the list.
So, "..." The content should be:
* Myfriendscallhisname
-- Question: is it the address pointing to myfriendscallhisname?
"*" Is it an address? Let's reverse it.
"&" Is the address.
"*" Indicates that the content is retrieved by address.
That is to say, the value of myfriendscallhisname is actually an address.
To put it bluntly, the iterator is a pointer.
However, if I say "The iterator is a pointer" at the beginning, it will scare many people. What is the pointer? Wow ...... Hard ......
The essence of the iterator is a pointer.
Is a pointer with a special purpose.
Okay, let's complete the entire program code:
# Include <iostream>
# Include <string>
# Include <list>
Using namespace STD;
Int main (INT argc, char * argv []) {
List <string> myfriends;
List <string >:: iterator myfriendscallhisname;
#
Myfriends. push_back ("Insects ");
Myfriends. push_back ("Soy Sauce ");
Myfriends. push_front ("Zhang Wuji ");
Myfriends. push_front (" ");
#
// Print the myfriends
For (myfriendscallhisname = myfriends. Begin ();
Myfriendscallhisname! = Myfriends. End ();
++ Myfriendscallhisname ){
// Dereference the iterator to get the element
Cout <* myfriendscallhisname <Endl;
}
System ("pause ");
Return 0;
}
Run it on your c ++ compiler.
By the way, let's see how the members in the list object are arranged.