Skip to content

Latest commit

 

History

History
63 lines (45 loc) · 1.31 KB

File metadata and controls

63 lines (45 loc) · 1.31 KB

operator[]

  • string[meta header]
  • std[meta namespace]
  • basic_string[meta class]
  • function[meta id-type]
const_reference operator[](size_type pos) const;          // (1) C++03
const_reference operator[](size_type pos) const noexcept; // (1) C++11

reference operator[](size_type pos);                      // (2) C++03
reference operator[](size_type pos) noexcept;             // (2) C++11

概要

pos 番目目の要素への参照を取得する。

要件

pos <= size()

戻り値

  • C++03

    • pos < size() の場合、*(begin() + pos) を返す。
    • pos == size()の場合、charT() の値を持ったオブジェクトへの参照を返す。
    • それ以外の場合は、未定義動作。
  • C++11以降

    • pos < size() の場合、*(begin() + pos) を返す。
    • そうでない場合は、charT() の値を持ったオブジェクトへの参照を返す。
    • 後者の場合、参照を変更するべきではない。

例外

投げない

計算量

定数時間

#include <iostream>
#include <string>

int main()
{
  std::string s = "hello";
  char& c = s[1];

  std::cout << c << std::endl;
}
  • s[1][color ff0000]

出力

e

参照