std::basic_string_view<CharT,Traits>::operator[]
提供: cppreference.com
<tbody>
</tbody>
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() を返すのではなく、未定義動作です。
例
Run this code
#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
関連項目
| 境界チェック付きで指定された文字にアクセスします (パブリックメンバ関数) |