GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • cpprefjp - C++日本語リファレンス
  • GitHub Project

This view

  • Original page
  • New address

GeezerVison is showing cpprefjp.github.io — std::vector::back - cpprefjp C++日本語リファレンス

  1. リファレンス
  2. vector
  3. vector
  4. back

最終更新日時: 2026年08月27日 13時51分57秒 (JST)
Akira Takahashi が更新

履歴 編集

function
<vector>

std::vector::back

reference back();                       // (1) C++98
constexpr reference back();             // (1) C++20

const_reference back() const;           // (2) C++98
constexpr const_reference back() const; // (2) C++20

概要

末尾要素への参照を取得する

戻り値

末尾の要素への参照を返す。

備考

この関数の効果は、以下と等価になる:

{
  auto tmp = end();
  --tmp;
  return *tmp;
}

計算量

定数時間

例

#include <iostream>
#include <vector>

int main()
{
  std::vector<int> v = {3, 1, 4};

  int& x = v.back();
  std::cout << x << std::endl;
}

出力

4

参照

  • P1004R2 Making std::vector constexpr
  • LWG Issue 1039. Sequence container back function should also support const_iterator
    • C++11で、要件表におけるback()の説明がiteratorを使うものからautoを使うものへ改められた。constなコンテナではend()がconst_iteratorを返すため、iteratorと書くとconst版の説明として成立しなかった
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.