reference back(); // (1) C++98
constexpr reference back(); // (1) C++26
const_reference back() const; // (2) C++98
constexpr const_reference back() const; // (2) C++26
概要
末尾要素への参照を取得する
戻り値
末尾の要素への参照を返す。
a.back() は { auto tmp = a.end(); --tmp; return *tmp; } と同じ結果になる。
計算量
定数時間
例
#include <iostream>
#include <list>
int main()
{
std::list<int> ls = {3, 1, 4};
int& x = ls.back();
std::cout << x << std::endl;
}
出力
4
参照
- P3372R3 constexpr containers and adaptors
- LWG Issue 1039. Sequence container
backfunction should also supportconst_iterator- C++11で、要件表における
back()の説明がiteratorを使うものからautoを使うものへ改められた。constなコンテナではend()がconst_iteratorを返すため、iteratorと書くとconst版の説明として成立しなかった
- C++11で、要件表における