Skip to content

Latest commit

 

History

History
47 lines (33 loc) · 700 Bytes

File metadata and controls

47 lines (33 loc) · 700 Bytes

operator[]

  • vector[meta header]
  • std[meta namespace]
  • vector[meta class]
  • function[meta id-type]
reference operator[](size_type n);             // (1)
const_reference operator[](size_type n) const; // (2)

概要

要素アクセス

戻り値

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

計算量

定数時間

備考

vector型のオブジェクトvに対して、v[n]*(v.begin() + n) は同じ結果になる。

#include <iostream>
#include <vector>

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

  // 2番目の要素を参照する
  int& x = v[2];
  std::cout << x << std::endl;
}

出力

4