std::basic_string_view<CharT,Traits>::operator[]
From cppreference.com
< cpp | string | basic string view
| constexpr const_reference operator[]( size_type pos ) const; |
(C++17以降) | |
指定された位置 pos の文字への const 参照を返します。
境界チェックは実行されません。 pos >= size() の場合、動作は未定義です。
目次 |
[編集] パラメータ
| pos | - | 返される文字の位置 |
[編集] 返り値
要求された文字への const 参照。
[編集] 例外
スローされません。
[編集] 計算量
定数。
[編集] 注記
std::basic_string::operator[] とは異なり、 std::basic_string_view::operator[](size()) は `CharT()` を返すのではなく、未定義の動作をします。
[編集] 例
このコードを実行
#include <iostream> #include <string_view> int main() { std::string str = "Exemplar"; std::string_view v = str; std::cout << v[2] << '\n'; // v[2] = 'y'; // Error: cannot modify through a string view str[2] = 'y'; std::cout << v[2] << '\n'; }
出力
e y
[編集] 関連項目
| 境界チェック付きで指定された文字にアクセスする (public member function) | |
| 指定された文字にアクセスする (public member function of std::basic_string<CharT,Traits,Allocator>) |