1. definition
the reverse iterator is an iterator that traverses containers in reverse order. That is, traverse the container from the last element to the first element. The reverse iterator reverses the meaning of auto-increment (and auto-increment): For the reverse iterator, The ++ operation accesses the previous element, while the -- operation accesses the next element.
2. function
(1) the inverse iterator must use the auto-subtraction OPERATOR: reverse_iterator on the standard container supports auto-increment operations, auto-Subtraction is also supported. However, the stream iterator cannot reverse traverse the stream, so the stream iterator cannot create a reverse iterator.
(2) You can use reverse_iterator: Base () to convert the reverse iterator to a normal iterator, and obtain the normal order from the reverse order. This is because some container member functions only accept parameters of the iterator type. Therefore, if you want to insert a new element at the position indicated by Ri, you cannot directly do this, because the insert function of vector does not accept reverse_iterator. The same problem occurs if you want to delete the elements at the position specified by Ri. Erase member functions reject reverse_iterator and insist on iterator. To complete the deletion and some forms of insert operations, you must first use the base function to convert reverse_iterator to iterator, and then use iterator to complete the work.
3. example
Void test_reverse () {int A [] = {-2,-1, 0, 1, 2, 3, 4}; STD: List <int> lst (, A + sizeof (a)/sizeof (INT); STD: Copy (lst. begin (), lst. end (), STD: ostream_iterator <int> (STD: cout, ""); STD: cout <STD: Endl; STD :: list <int>: reverse_iterator rit = lst. rbegin (); While (rit! = Lst. rend () STD: cout <* rit ++ <""; STD: cout <STD: Endl; // use base () implement insert or erase operations. STD: vector <int> vect (A, A + sizeof (a)/sizeof (INT); // The Reverse iterator points to 2std: vector <int> :: reverse_iterator vrit = STD: Find (vect. rbegin (), vect. rend (), 2); // note: the forward iterator points to 3std: vector <int >:: iterator it (vrit. base (); inserter (vect, It) = 10; STD: Copy (vect. begin (), vect. end (), STD: ostream_iterator <int> (STD: cout, ""); STD: cout <STD: Endl ;}
Output result:
-2-1 0 1 2 3 44 3 2 1 0-1-2-2-1 0 1 2 10 3 4 press any key to continue...