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

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

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

履歴 編集

function
<array>

std::array::at(C++11)

reference at(size_type n);                       // (1) C++11
constexpr reference at(size_type n);             // (1) C++17

const_reference at(size_type n) const;           // (2) C++11
constexpr const_reference at(size_type n) const; // (2) C++14

概要

n番目の要素を参照する。

戻り値

a.at(n)はn番目の要素への参照を返す。もし、aがconstだった場合には、n番目の要素へのconst参照を返す。

例外

n >= a.size()だった場合にはout_of_range例外を投げる。

計算量

定数時間

備考

この関数は、例外を送出しうるため、フリースタンディング処理系では削除される(フリースタンディング処理系では使用できない)。

a.at(n) は *(a.begin() + n) と同じ結果になる。

例

#include <iostream>
#include <array>

int main()
{
  std::array<int, 3> ar = {3, 1, 4};
  const std::array<int, 3>& car = ar;

  int& a = ar.at(2);
  const int& b = car.at(2);

  std::cout << a << std::endl;
  std::cout << b << std::endl;

  try {
    ar.at(999); // 範囲外アクセス
  }
  catch (std::out_of_range& e) {
    std::cout << "out of range" << std::endl;
  }
}

出力

4
4
out of range

バージョン

言語

  • C++11

処理系

  • Clang: 9.0.1 ✅
  • GCC: 4.7.0 ✅
  • Visual C++: 2008 (std::tr1) ✅, 2010 ✅, 2012 ✅

参照

  • N3470 Constexpr Library Additions: containers, v2
  • P0031R0 A Proposal to Add Constexpr Modifiers to reverse_iterator, move_iterator, array and Range Access
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.