最終更新日時(UTC):
が更新

履歴 編集

function
<array>

std::array::operator[](C++11)

reference operator[](size_type n);                       // (1) C++11
constexpr reference operator[](size_type n);             // (1) C++17

const_reference operator[](size_type n) const;           // (2) C++11
constexpr const_reference operator[](size_type n) const; // (2) C++14

概要

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

戻り値

a[n]n番目の要素への参照を返す。aconstである場合にはconst参照を返す。

計算量

定数時間

備考

a[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[2];
  const int& b = car[2];

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

出力

4
4

バージョン

言語

  • C++11

処理系

参照