[STL] Reverse iterator)

Source: Internet
Author: User

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...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.