const charT& back() const; // (1) C++11
constexpr const charT& back() const; // (1) C++20
charT& back(); // (2) C++11
constexpr charT& back(); // (2) C++20
概要
末尾要素への参照を取得する。
堅牢化された事前条件
戻り値
operator[](size() - 1) の結果を返す。
例
#include <iostream>
#include <string>
int main()
{
std::string s = "hello";
char& c = s.back();
std::cout << c << std::endl;
}
出力
o
参照
- LWG Issue 534. Missing
basic_stringmembers - P0980R1 Making
std::stringconstexpr - P3471R4 Standard library hardening
- P3878R1 Standard library hardening should not use the 'observe' semantic
- LWG Issue 1039. Sequence container
backfunction should also supportconst_iterator- C++11で、要件表における
back()の説明がiteratorを使うものからautoを使うものへ改められた。constなコンテナではend()がconst_iteratorを返すため、iteratorと書くとconst版の説明として成立しなかった
- C++11で、要件表における