list::back
リストの最後の要素への参照を返します。
reference back( ); const_reference back( ) const;
戻り値
リストの最後の要素。 リストが空の場合、戻り値は定義されません。
解説
back の戻り値が const_reference に割り当てられている場合、リスト オブジェクトを変更することはできません。 back の戻り値が reference に割り当てられている場合、リスト オブジェクトを変更できます。
_SECURE_SCL 1 でのコンパイル時に、空のリスト内の要素にアクセスしようとすると、ランタイム エラーが発生します。 詳細については、「チェックを行う反復子」を参照してください。
使用例
// list_back.cpp
// compile with: /EHsc
#include <list>
#include <iostream>
int main( )
{
using namespace std;
list <int> c1;
c1.push_back( 10 );
c1.push_back( 11 );
int& i = c1.back( );
const int& ii = c1.front( );
cout << "The last integer of c1 is " << i << endl;
i--;
cout << "The next-to-last integer of c1 is " << ii << endl;
}
必要条件
ヘッダー: <list>
名前空間: std