Share via


list::rend

逆順のリスト内の最後の要素の次の場所を指す反復子を返します。

const_reverse_iterator rend( ) const; 
reverse_iterator rend( );

戻り値

逆順のリスト内の最後の要素の次の場所 (通常の順序のリスト内の最初の要素の前の場所) を指す逆順双方向反復子。

解説

rend は、end がリストで使用されるように、逆順のリストで使用されます。

rend の戻り値が const_reverse_iterator に割り当てられている場合、リスト オブジェクトを変更することはできません。 rend の戻り値が reverse_iterator に割り当てられている場合、リスト オブジェクトを変更できます。

rend を使用して、逆順反復子がリストの末尾に達したかどうかをテストできます。

rend によって返された値は逆参照しないでください。

使用例

// list_rend.cpp
// compile with: /EHsc
#include <list>
#include <iostream>

int main( ) 
{
   using namespace std;
   list <int> c1;
   list <int>::iterator c1_Iter;
   list <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
   // list <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 list (to point to the first element here)
   cout << "The first element in the list is: " << *c1_rIter << endl;

   cout << "The list 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 list
   cout << "The reversed list 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 list (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 list is:";
   for ( c1_rIter = c1.rbegin( ); c1_rIter != c1.rend( ); c1_rIter++ )
      cout << " " << *c1_rIter;
   cout << endl;
}
  

必要条件

ヘッダー: <list>

名前空間: std

参照

関連項目

list クラス

標準テンプレート ライブラリ