共用方式為


set::rend

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

const_reverse_iterator rend( ) const; 
reverse_iterator rend( );

Return Value

A reverse bidirectional iterator that addresses the location succeeding the last element in a reversed set (the location that had preceded the first element in the unreversed set).

Remarks

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

If the return value of rend is assigned to a const_reverse_iterator, then the set object cannot be modified. If the return value of rend is assigned to a reverse_iterator, then the set object can be modified. The value returned by rend should not be dereferenced.

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

Example

// set_rend.cpp
// compile with: /EHsc
#include <set>
#include <iostream>

int main() {
   using namespace std;   
   set <int> s1;
   set <int>::iterator s1_Iter;
   set <int>::reverse_iterator s1_rIter;
   set <int>::const_reverse_iterator s1_crIter;

   s1.insert( 10 );
   s1.insert( 20 );
   s1.insert( 30 );

   s1_rIter = s1.rend( );
   s1_rIter--;
   cout << "The last element in the reversed set is "
        << *s1_rIter << "." << endl;

   // end can be used to terminate an iteration 
   // throught a set in a forward order
   cout << "The set is: ";
   for ( s1_Iter = s1.begin( ) ; s1_Iter != s1.end( ); s1_Iter++ )
      cout << *s1_Iter << " ";
   cout << "." << endl;

   // rend can be used to terminate an iteration 
   // throught a set in a reverse order
   cout << "The reversed set is: ";
   for ( s1_rIter = s1.rbegin( ) ; s1_rIter != s1.rend( ); s1_rIter++ )
      cout << *s1_rIter << " ";
   cout << "." << endl;

   s1_rIter = s1.rend( );
   s1_rIter--;
   s1.erase ( *s1_rIter );

   s1_rIter = s1.rend( );
   --s1_rIter;
   cout << "After the erasure, the last element in the "
        << "reversed set is " << *s1_rIter << "." << endl;
}

Output

The last element in the reversed set is 10.
The set is: 10 20 30 .
The reversed set is: 30 20 10 .
After the erasure, the last element in the reversed set is 20.

Requirements

Header: <set>

Namespace: std

See Also

Reference

set Class

set::rbegin and set::rend

Standard Template Library