Rbegin method in STL vector (5)

Source: Internet
Author: User
Public member function <vector> STD: vector: rbegin
  • C ++ 98
  • C ++ 11
      reverse_iterator rbegin() noexcept;const_reverse_iterator rbegin() const noexcept;
Return reverse iterator to reverse beginning returns a reverse first element. Example: # include <iostream>
# Include <vector>
# Include <iterator>
Using namespace STD;
Int main ()
{
Vector <int> VI;
Vi. push_back (5 );
Vi. push_back (999 );
Cout <* VI. rbegin () <Endl;




}
Running result:
We can see that the element arrangement in VI is {5,999}, and the iterator returned by rbegin () points to the position of the last element, but this position and end () the end () points to the last element and then the next position (supertail iterator ).

ReturnsReverse iteratorPointing to the last element in the vector (I. e., itsReverse beginning).

Returns a reverse iterator pointing to the last element (equivalent to looking forward from the back)


Reverse iteratorsIterate backwards: increasing them moves them towards the beginning of the container.

Backward iteration of the reverse iterator: When the reverse iterator is added, the iterator actually moves to the beginning of the container.

For example:

# Include <iostream>
# Include <vector>
# Include <iterator>
Using namespace STD;
Int main ()
{
Vector <int> VI;
Vi. push_back (5 );
Vi. push_back (999 );
Vi. push_back (222 );
Vector <int>: reverse_iterator Vr = VI. rbegin ();
Cout <* VR <Endl;
VR ++;
Cout <* VR <Endl;




}

Result:


As you can see, after VR ++, VR is actually moving forward!


RbeginPoints to the element right before the one that wocould be pointed to by member end.

The element that rbegin points to is exactly the first one that end () points.


Notice that unlike Member vector: Back, which returns a reference to this same element, this function returns Reverse Random Access iterator.

Unlike back (), back () returns a reference, which returns a reverse Random Access iterator.


Parametersnone

Return Value

Return Value:

A reverse iterator toReverse beginningOf the sequence container.

Returns a reverse iterator that points to the beginning of the reverse direction of the ordered container.


If the vector object is const-qualified, the function returnsConst_reverse_iterator. Otherwise, it returnsReverse_iterator.

If the vector object has the const attribute, the iterator returned by this method also has the const attribute. Otherwise, a common reverse_iterator is returned.


Member types Reverse_iteratorAnd Const_reverse_iteratorAre reverse Random Access iterator types (pointing to an element and to a const element, respectively). See vector member types.

Reverse_iterator is a reverse Random Access iterator.


Example
123456789101112131415161718192021
// vector::rbegin/rend#include <iostream>#include <vector>int main (){  std::vector<int> myvector (5);  // 5 default-constructed ints  std::vector<int>::reverse_iterator rit = myvector.rbegin();  int i=0;  for (rit = myvector.rbegin(); rit!= myvector.rend(); ++rit)    *rit = ++i;  std::cout << "myvector contains:";  for (std::vector<int>::iterator it = myvector.begin(); it != myvector.end(); ++it)    std::cout << ‘ ‘ << *it;  std::cout << ‘\n‘;  return 0;}
Edit & run


Output:
myvector contains: 5 4 3 2 1

Complexityconstant.

Iterator validityno changes.

This method does not affect the validity of other iterators.


Data races

The container is accessed (neither the const nor the non-const versions modify the container ).

This method does not modify the content in the container.


No contained elements are accessed by the call, but the iterator returned can be used to access or modify elements. Concurrently accessing or modifying different elements is safe.

This method does not access the elements of container Lee, but the returned iterator can be used to access and modify elements, and is safe.


Exception safety

No-throw guarantee:This member function never throws exceptions.

This method does not throw an exception.

The copy construction or assignment of the returned iterator is also guaranteed to never throw.

The iterator obtained by using the copy constructor or the value assignment operator does not throw an exception.


// For more information about the deficiencies, see the source: Click to open the link.

Personal blog homepage: Click to open the link

Yu gdut




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.