list::rbegin
逆順のリスト内の最初の要素を示す反復子を返します。
const_reverse_iterator rbegin( ) const;
reverse_iterator rbegin( );
戻り値
逆順のリスト内の最初の要素を指す反転双方向反復子 (または通常の順序のリスト内の最後の要素だったものを指す反転双方向反復子)。
解説
rbegin は、begin がリストで使用されるように、逆順のリストで使用されます。
rbegin の戻り値が const_reverse_iterator に割り当てられている場合、リスト オブジェクトを変更することはできません。 rbegin の戻り値が reverse_iterator に割り当てられている場合、リスト オブジェクトを変更できます。
rbegin を使用して、リスト内を後方に向かって反復処理できます。
使用例
// list_rbegin.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 replaced the line above, *c1_rIter = 40;
// (below) would be an error
//list <int>::const_reverse_iterator c1_rIter;
c1.push_back( 10 );
c1.push_back( 20 );
c1.push_back( 30 );
c1_rIter = c1.rbegin( );
cout << "The last 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;
// rbegin can be used to start an iteration through a list in
// reverse order
cout << "The reversed list is:";
for ( c1_rIter = c1.rbegin( ); c1_rIter != c1.rend( ); c1_rIter++ )
cout << " " << *c1_rIter;
cout << endl;
c1_rIter = c1.rbegin( );
*c1_rIter = 40;
cout << "The last element in the list is now " << *c1_rIter << "." << endl;
}
必要条件
ヘッダー: <list>
名前空間: std