Talking about the base () function of reverse_iterator

Source: Internet
Author: User

Non original, original link: http://blog.csdn.net/shuchao/article/details/3705252

The base member function that calls reverse_iterator can generate the "corresponding" iterator. However, this sentence is a bit disappointing. For example, let's take a look at this code. First, we put numbers 1-5 into a vector, then generate a reverse_iterator pointing to 3, and initialize an iterator through the base of reverse_iterator:

Vector <int> v; v. reserve (5); for (int I = 1; I <= 5; ++ I) {// insert 1 to 5 v to the vector. push_back (I);} vector <int>: reverse_iterator ri = find (v. rbegin (), v. rend (), 3); vector <int>: iterator I (ri. base (); // make the base of I and ri the same

After executing the above code, you can think of the results as follows:

This figure shows the specific offset between the reverse_iterator and its corresponding base iterator, just like the related begin () and end, but I didn't say anything you need to know. In particular, it does not explain how to implement the operations you want to perform on ri. 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.

Let us assume that you want to insert a new element v at the position specified by ri. Specifically, we assume that the value you want to insert is 99. Remember that the order in which ri is traversed is from right to left, and the insert operation inserts new elements into the ri position, and moves the elements in the original ri position to the next position in the traversal process, we think 3 should appear on the left side of 99. After the insert operation, v looks like this:

Of course, we cannot use ri to specify the insert location because it is not an iterator. We must use I instead. As mentioned above, when ri points to 3, I (that is, ri. base () points to 4. If we use ri to specify the insert position, we use I to point to the insert position. That assumption is correct. What is the conclusion?

To insert a new element at the position specified by a reverse_iterator ri, just insert it at the position pointed by ri. base. For insert operations, ri and ri. base () are equivalent, and ri. base () are actually iterator corresponding to ri. Now let's consider deleting elements. Review the relationship between ri and I in the initial vector (that is, before inserting 99:

If you want to delete the elements pointed to by ri, you cannot directly use I, because I and ri do not point to the same element. Therefore, you need to delete the first element of I. To delete an element at the position specified by a reverse_iterator ri, delete the previous element of ri. base. For delete operations, ri and ri. base () are not equivalent, and ri. base () is not the iterator corresponding to ri. We still need to look at the delete operation code, because it is quite surprising.

Vector <int> v; // insert 1 to 5 to v, same as vecot <int>: reverse_iterator ri = find (v. rbegin (), v. rend (), 3); // same as above, ri points to 3v. erase (-- ri. base (); // try to delete ri. elements before base (). For vector, compilation fails.

This design does not have any problems. Expression -- ri. base () indicates the elements that need to be deleted. Furthermore, they can process all the containers except vector and string, and it may also be able to process vector and string, but it cannot process most implementations of vector and string. In this implementation, iterator (and const_iterator) will use built-in pointers, so the result of ri. base () is a pointer. Both C and C ++ stipulate that the pointer returned by the function cannot be directly modified. Therefore, the iterator of string and vector is the STL platform of the pointer, such as -- ri. expressions such as base () cannot be compiled. To remove an element from a position specified by reverse_iterator, avoid modifying the return value of the base. No problem. If you cannot reduce the return value of the base call, you only need to add the reverse_iterator value before calling the base.

// Same as v. erase (++ ri). base (); // Delete the elements pointed to by ri; // This compilation is okay!

Because this method applies to all standard containers, This is the preferred technique for deleting an element pointed out by reverse_iterator.

It is clear that the base member function of reverse_iterator returns a "corresponding" iterator statement. This is true for insert operations, but not for delete operations. When you need to convert reverse_iterator to iterator, it is very important that you know how to handle the returned iterator, because only in this way can you decide whether the iterator you get is what you need.

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.