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

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

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

履歴 編集

function
<string>

std::basic_string::at

const_reference at(size_type pos) const;           // (1) C++98
constexpr const_reference at(size_type pos) const; // (1) C++20

reference at(size_type pos);                       // (2) C++98
constexpr reference at(size_type pos);             // (2) C++20

概要

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

戻り値

operator[](pos) の結果。

例外

pos >= size() の時、out_of_range 例外を投げる。

例

#include <iostream>
#include <string>
#include <stdexcept>

int main()
{
  std::string s = "hello";

  char& c = s.at(1);
  std::cout << c << std::endl;

  try {
    s.at(5);
  }
  catch (std::out_of_range&) {
    std::cout << "access error" << std::endl;
  }
}

出力

e
access error

参照

  • LWG Issue 2207. basic_string::at should not have a Requires clause
    • C++11まで、この関数を呼び出す要件として「pos < size()」があったが、例外節があるため要件節は不要。C++14で要件節が削除された。
  • P0980R1 Making std::string 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.