Namespaces
Variants

std::basic_string_view<CharT,Traits>:: at

From cppreference.net
constexpr const_reference at ( size_type pos ) const ;
(C++17以降)

指定された位置 pos の文字への const 参照を返します。境界チェックが実行され、無効なアクセスの場合には std::out_of_range 型の例外がスローされます。

目次

パラメータ

pos - 返される文字の位置

戻り値

Const 要求された文字への参照。

例外

std::out_of_range がスローされる条件: pos >= size ( ) の場合。

計算量

定数。

#include <iostream>
#include <stdexcept>
#include <string_view>
int main()
{
    std::string_view str_view("abcdef");
    try
    {
        for (std::size_t i = 0; true; ++i)
            std::cout << i << ": " << str_view.at(i) << '\n';
    }
    catch (const std::out_of_range& e)
    {
        std::cout << "Whooops. Index is out of range.\n";
        std::cout << e.what() << '\n';
    }
}

出力例:

0: a
1: b
2: c
3: d
4: e
5: f
6: Whooops. Index is out of range.
basic_string_view::at: __pos (which is 6) >= this->size() (which is 6)

関連項目

指定された文字にアクセスする
(公開メンバ関数)
境界チェック付きで指定された文字にアクセスする
( std::basic_string<CharT,Traits,Allocator> の公開メンバ関数)