名前空間
変種
操作

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

提供: cppreference.com
 
 
 
 
constexpr const_reference at(size_type pos)const;
(C++17以上)

指定された位置 pos の文字を指す const 参照を返します。 範囲チェックが行われます。 無効なアクセスの場合は std::out_of_range 型の例外が投げられます。

目次

[編集]引数

pos - 返す文字の位置

[編集]戻り値

要求された文字を指す const 参照。

[編集]例外

pos >= size() の場合、 std::out_of_range を投げます。

[編集]計算量

一定。

[編集]

#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(conststd::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)

[編集]関連項目

指定された文字にアクセスします
(パブリックメンバ関数)[edit]
close