The iterator in STL is similar to a pointer, but not just a pointer. The iterator is similar to the pointer, and its function is similar to a pointer. However, the iterator returns a value from the center of the container by reloading the "*" and "->" of a dollar. It is not a good idea to store these values in the container, because these values will become invalid whenever a new value is added to the container or a value is deleted from the container. To some extent, the iterator can be considered as a handle ). Generally, the iterator type can change, so containers may change in several different ways: iterator -- for any container except vector, you can use this iterator to take a step forward to the container in one operation. This means that you can only use the "++" Operator for such iterators. You cannot use the "--" or "+ =" operators. For the vector container, you can use any operator in "+ =", "-", "+ +", "-=", and "<", "<=", "> ", "> =", "=", "! = "And other comparison operators. Reverse_iterator -- if you want to use an iterator in the backward direction instead of the forward direction to traverse elements in containers other than the vector, you can use reverse_iterator to reverse the traversal direction, you can also use rbegin () to replace begin () and rend () to replace end (). At this time, the "++" operator will traverse the back direction.
Const_iterator -- a forward iterator that returns a constant value. You can use this type of iterator to point to a read-only value.
Const_reverse_iterator is an iterator that traverses in the opposite direction. It returns a constant value.
Hybrid iterator Functions
In operations involving containers and algorithms, there are also two iterator functions that are very useful:
· Advance () increases or decreases the iterator according to the specified number.
Template <class InIt, class Dist>
Void advance (InIt & it, Dist n );
When n is positive, it increases the iterator passed by the first parameter. When n is negative, it decreases the iterator passed.
· Distance () returns the number of (incremental) operations required to reach an iterator.
Template <class Init, class Dist>
Dist distance (InIt first, InIt last, Dist & n );
Distance is an overloaded name; there are actually two distance functions.
Template <class InputIterator>
Inline iterator_traits <InputIterator>: difference_type distance (InputIterator first, InputIterator last );
Template <class InputIterator, class Distance>
Void distance (InputIterator first, InputIterator last, Distance & n );
Compare the number of two iterators before and after.
For example:
List ilist; List: iterator P = find (ilist. begin (), ilist. end (), 2); cout <"before: P =" <* P <Endl; advance (p, 2 ); // same as P = P + 2; cout <"after: P =" <* P <Endl; int K = 0; distance (p, ilist. end (), k); cout <"k =" <k <Endl; // returns the number between the two.
The advance () function accepts two parameters. The second parameter is the number of advances. For the prepush iterator, This value must be positive, and for the bidirectional iterator and the random access iterator, this value can be negative.
Use the distance () function to return the steps required to reach another iterator. Note that the distance () function is iterative, that is, it increments by the third parameter. Therefore, you must initialize this parameter. If this parameter is not initialized, it is almost doomed to fail.
Distance () Sample Code:
// distance.cpp// compile with: /EHsc#pragma warning (disable:4786)#include <iostream>#include <vector>#include <iterator>#include <string>using namespace std;typedef vector<string > VTRLIST;int main() { VTRLIST Vector; VTRLIST::iterator iVector; VTRLIST::difference_type dTheDiff; Vector.push_back("A1"); Vector.push_back("B2"); Vector.push_back("C3"); Vector.push_back("D4"); Vector.push_back("E5"); Vector.push_back("F6"); Vector.push_back("G7"); // Print out the list iVector=Vector.begin(); cout << "The list is: "; for (int i = 0; i < 7 ; i++, iVector++) cout << *iVector << " "; // Initialize the iterator the first element" iVector=Vector.begin(); cout << "/n/nAdvance to the 3rd element." << endl; advance( iVector, 2); cout << "The element is " << *iVector << endl; dTheDiff = distance( Vector.begin(), iVector); cout << "The distance from the beginning is " << dTheDiff << endl; cout << "Calculate it in reverse order " << endl; dTheDiff = distance( iVector, Vector.begin()); cout << "The distance is " << dTheDiff << endl; cout << "/nUse distance() to count from the 3rd element to the end." << endl; dTheDiff = distance( iVector, Vector.end()); // Note that end() returns one past the end of the sequence cout << "The distance is " << dTheDiff << endl; cout <<"/nUse distance() to count the total length." << endl; dTheDiff = distance( Vector.begin(), Vector.end() ); cout << "The total distance is " << dTheDiff << endl;}/*Output The list is: A1 B2 C3 D4 E5 F6 G7 Advance to the 3rd element.The element is C3The distance from the beginning is 2Calculate it in reverse order The distance is -2Use distance() to count from the 3rd element to the end.The distance is 5Use distance() to count the total length.The total distance is 7*/
advance() Sample Code:
// Advance. cpp // compile with:/ehs# pragma warning (disable: 4786) # include <iostream> # include <string> # include <list> using namespace std; typedef list <string> STRLIST; int main () {STRLIST List; STRLIST: iterator iList; STRLIST: difference_type dTheDiff; List. push_back ("A1"); List. push_back ("B2"); List. push_back ("C3"); List. push_back ("D4"); List. push_back ("E5"); List. push_back ("F6"); List. push_back ("G7"); // Print out the list iList = List. begin (); cout <"The list is:"; for (int I = 0; I <7; I ++, iList ++) cout <* iList <""; // Initialize to the first element "iList = List. begin (); cout <"/n/nAdvance to the 3rd element. "<endl; advance (iList, 2); cout <" The element is "<* iList <endl ;}/*Output The list is: A1 B2 C3 D4 E5 F6 G7 Advance to the 3rd element.The element is C3*/
Of course, advance () and distance () can also be used to convert const_iterator to iterator.
Reference: Invalid STL Article 27: Converting const_iterator into iterator using distance and advance
In addition, if you use the reverse iterator of the STL container, "Article 28: Learn How to Get iterator through the reverse_iterator base" should also be well understood.