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