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::at - cpprefjp C++日本語リファレンス

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

最終更新日時: 2026年10月03日 22時31分04秒 (JST)
Akira Takahashi が更新

履歴 編集

function
<vector>

std::vector::at

reference at(size_type n);                       // (1) C++98
constexpr reference at(size_type n);             // (1) C++20

const_reference at(size_type n) const;           // (2) C++98
constexpr const_reference at(size_type n) const; // (2) C++20

概要

要素アクセス

戻り値

n番目の要素への参照を返す。

計算量

定数時間

例外

n >= size()の場合、out_of_range例外を送出する。

備考

at(n) は *(begin() + n) と同じ結果となるが、イテレータを使用したランダムアクセスやoperator[]と違い、コンテナの要素数に対して境界チェックを行う。

例

#include <iostream>
#include <vector>
#include <stdexcept>

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

  // 2番目の要素を参照する
  int& x = v.at(2);
  std::cout << x << std::endl;

  // 範囲外アクセスした場合、例外が投げられる
  try {
    int& y = v.at(3);
  }
  catch (std::out_of_range& ex) {
    std::cout << "out of range" << std::endl;
  }
}

出力

4
out of range

参照

  • P1004R2 Making std::vector constexpr
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.