Compartilhar via


deque::rend

Returns an iterator that addresses the location succeeding the last element in a reversed deque.

const_reverse_iterator rend( ) const; 
reverse_iterator rend( );

Return Value

A reverse random-access iterator that addresses the location succeeding the last element in a reversed deque (the location that had preceded the first element in the unreversed deque).

Remarks

rend is used with a reversed deque just as end is used with a deque.

If the return value of rend is assigned to a const_reverse_iterator, the deque object cannot be modified. If the return value of rend is assigned to a reverse_iterator, the deque object can be modified.

rend can be used to test whether a reverse iterator has reached the end of its deque.

The value returned by rend should not be dereferenced.

Example

// deque_rend.cpp
// compile with: /EHsc
#include <deque>
#include <iostream>

int main( ) 
{
   using namespace std;

   deque <int> c1;
   deque <int>::iterator c1_Iter;
   deque <int>::reverse_iterator c1_rIter;
   // If the following line had replaced the line above, an error
   // would have resulted in the line modifying an element
   // (commented below) because the iterator would have been const
   // deque <int>::const_reverse_iterator c1_rIter;
   
   c1.push_back( 10 );
   c1.push_back( 20 );
   c1.push_back( 30 );

   c1_rIter = c1.rend( );
   c1_rIter --; // Decrementing a reverse iterator moves it forward 
                // in the deque (to point to the first element here)
   cout << "The first element in the deque is: " << *c1_rIter << endl;

   cout << "The deque is: ";
   for ( c1_Iter = c1.begin( ); c1_Iter != c1.end( ); c1_Iter++ )
      cout << *c1_Iter << " ";
   cout << endl;

   // rend can be used to test if an iteration is through all of 
   // the elements of a reversed deque
   cout << "The reversed deque is: ";
   for ( c1_rIter = c1.rbegin( ); c1_rIter != c1.rend( ); c1_rIter++ )
      cout << *c1_rIter << " ";
   cout << endl;

   c1_rIter = c1.rend( );
   c1_rIter--; // Decrementing the reverse iterator moves it backward 
               // in the reversed deque (to the last element here)
   *c1_rIter = 40; // This modification of the last element would 
                   // have caused an error if a const_reverse 
                   // iterator had been declared (as noted above)
   cout << "The modified reversed deque is: ";
   for ( c1_rIter = c1.rbegin( ); c1_rIter != c1.rend( ); c1_rIter++ )
      cout << *c1_rIter << " ";
   cout << endl;
}
The first element in the deque is: 10
The deque is: 10 20 30 
The reversed deque is: 30 20 10 
The modified reversed deque is: 30 20 40 

Requirements

Header: <deque>

Namespace: std

See Also

Reference

deque Class

deque::rbegin and deque::rend

Standard Template Library