"C + + Primer The 10th chapter" re-Explore Iterators

Source: Internet
Author: User

Reverse iterator

• A reverse iterator is an iterator that moves backwards from the tail element to the first element in the container. For reverse iterators, the meaning of the increment (and decrement) operation is reversed.

• Incrementing a reverse iterator (++it) moves to the previous element; decrements an iterator (--it) moves to the next element.

• Other containers support Reverse iterators in addition to Forward_list. We can get a reverse iterator by calling the Rbegin, Rcend, Crbegin, and Crend member functions. These member functions return an iterator that points to the end element of the container and the previous position of the first element. As with ordinary iterators, reverse iterators also have const and non-const versions.

1#include <iostream>2#include <vector>3#include <iterator>4 using namespacestd;5 6 intMain ()7 {8vector<int> VEC = {0,1,2,3,4,5,6,7,8,9 };9      for(Auto R_iter = Vec.crbegin (); R_iter! = Vec.crend (); + +r_iter)Tencout << *r_iter <<" ";  Onecout <<Endl; A     return 0; -}

Output Result:

While reversing the meaning of the increment and decrement operators can be confusing, doing so is a way for us to transparently process the container forward or backward with an algorithm. For example, you can sort vectors into descending order by passing a pair of reverse iterators to sort:

Sort (Vec.begin (), Vec.end ()); Sort (Vec.rbegin (), Vec.rend ());

1#include <iostream>2#include <deque>3#include <algorithm>4#include <iterator>5 using namespacestd;6 7 voidPrintintelem)8 {9cout << Elem <<' ';Ten } One  A intMain () - { -deque<int>Coll; the      for(inti =1; I <=9; ++i) - Coll.push_back (i); -  -deque<int>:: iterator pos1; +POS1 = Find (Coll.begin (), Coll.end (),2); -  +deque<int>:: iterator Pos2; APos2 = Find (Coll.begin (), Coll.end (),7); at For_each (POS1, Pos2, print); -cout <<Endl; -  -deque<int>:: Reverse_iterator rpos1 (POS1); -deque<int>:: Reverse_iterator rpos2 (POS2); - For_each (Rpos2, rpos1, print); incout <<Endl; -     return 0; to}

Output Result:

Analysis

The code first inserts 1 to 9 in a deque, then looks for the position of the element value 2 and 7, assigns the value to the iterator pos1 and the Pos2, and then outputs, because the operation in the STL is always left-open, that is, [2,7], so the output 2 3 4 5 6,7 will not be output.

Next, the iterator is converted to a reverse iterator, output again, for the reverse iterator, because it is reversed, so logically it is left open right closed (here I tried rpos2 for Iterator.end (), RPOS1 for Iterator.begin (), at this time output all), that is ( 7,2] (in fact, left and right open, just the left and iterator order). So output 6 5 4 3 2, the picture below explains very clearly.

"C + + Primer The 10th chapter" re-Explore Iterators

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.